2
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
25
#include "precompiled.hpp"
26
#include "utilities/ostream.hpp"
27
#include "windbghelp.hpp"
31
typedef DWORD (WINAPI *pfn_SymSetOptions)(DWORD);
32
typedef DWORD (WINAPI *pfn_SymGetOptions)(void);
33
typedef BOOL (WINAPI *pfn_SymInitialize)(HANDLE, PCTSTR, BOOL);
34
typedef BOOL (WINAPI *pfn_SymGetSymFromAddr64)(HANDLE, DWORD64, PDWORD64, PIMAGEHLP_SYMBOL64);
35
typedef DWORD (WINAPI *pfn_UnDecorateSymbolName)(const char*, char*, DWORD, DWORD);
36
typedef BOOL (WINAPI *pfn_SymSetSearchPath)(HANDLE, PCTSTR);
37
typedef BOOL (WINAPI *pfn_SymGetSearchPath)(HANDLE, PTSTR, int);
38
typedef BOOL (WINAPI *pfn_SymRefreshModuleList)(HANDLE);
39
typedef BOOL (WINAPI *pfn_StackWalk64)(DWORD MachineType,
42
LPSTACKFRAME64 StackFrame,
44
PREAD_PROCESS_MEMORY_ROUTINE64 ReadMemoryRoutine,
45
PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
46
PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
47
PTRANSLATE_ADDRESS_ROUTINE64 TranslateAddress);
48
typedef PVOID (WINAPI *pfn_SymFunctionTableAccess64)(HANDLE hProcess, DWORD64 AddrBase);
49
typedef DWORD64 (WINAPI *pfn_SymGetModuleBase64)(HANDLE hProcess, DWORD64 dwAddr);
50
typedef BOOL (WINAPI *pfn_MiniDumpWriteDump) (HANDLE hProcess, DWORD ProcessId, HANDLE hFile,
51
MINIDUMP_TYPE DumpType, PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
52
PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
53
PMINIDUMP_CALLBACK_INFORMATION CallbackParam);
54
typedef BOOL (WINAPI *pfn_SymGetLineFromAddr64) (HANDLE hProcess, DWORD64 dwAddr,
55
PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line);
56
typedef LPAPI_VERSION (WINAPI *pfn_ImagehlpApiVersion)(void);
58
// Add functions as needed.
59
#define FOR_ALL_FUNCTIONS(DO) \
60
DO(ImagehlpApiVersion) \
64
DO(SymGetSymFromAddr64) \
65
DO(UnDecorateSymbolName) \
66
DO(SymSetSearchPath) \
67
DO(SymGetSearchPath) \
69
DO(SymFunctionTableAccess64) \
70
DO(SymGetModuleBase64) \
71
DO(MiniDumpWriteDump) \
72
DO(SymGetLineFromAddr64) \
73
DO(SymRefreshModuleList)
76
#define DECLARE_FUNCTION_POINTER(functionname) \
77
static pfn_##functionname g_pfn_##functionname;
79
FOR_ALL_FUNCTIONS(DECLARE_FUNCTION_POINTER)
82
static HMODULE g_dll_handle = nullptr;
83
static DWORD g_dll_load_error = 0;
84
static API_VERSION g_version = { 0, 0, 0, 0 };
87
state_uninitialized = 0,
90
} g_state = state_uninitialized;
92
static void initialize() {
94
assert(g_state == state_uninitialized, "wrong sequence");
95
g_state = state_error;
97
g_dll_handle = ::LoadLibrary("DBGHELP.DLL");
98
if (g_dll_handle == nullptr) {
99
g_dll_load_error = ::GetLastError();
101
// Note: We loaded the DLL successfully. From here on we count
102
// initialization as success. We still may fail to load all of the
103
// desired function pointers successfully, but DLL may still be usable
104
// enough for our purposes.
105
g_state = state_ready;
107
#define DO_RESOLVE(functionname) \
108
g_pfn_##functionname = (pfn_##functionname) ::GetProcAddress(g_dll_handle, #functionname);
110
FOR_ALL_FUNCTIONS(DO_RESOLVE)
112
// Retrieve version information.
113
if (g_pfn_ImagehlpApiVersion) {
114
const API_VERSION* p = g_pfn_ImagehlpApiVersion();
115
memcpy(&g_version, p, sizeof(API_VERSION));
122
///////////////////// External functions //////////////////////////
124
// All outside facing functions are synchronized. Also, we run
125
// initialization on first touch.
127
static CRITICAL_SECTION g_cs;
129
namespace { // Do not export.
130
class WindowsDbgHelpEntry {
132
WindowsDbgHelpEntry() {
133
::EnterCriticalSection(&g_cs);
134
if (g_state == state_uninitialized) {
138
~WindowsDbgHelpEntry() {
139
::LeaveCriticalSection(&g_cs);
144
// Called at DLL_PROCESS_ATTACH.
145
void WindowsDbgHelp::pre_initialize() {
146
::InitializeCriticalSection(&g_cs);
149
DWORD WindowsDbgHelp::symSetOptions(DWORD arg) {
150
WindowsDbgHelpEntry entry_guard;
151
if (g_pfn_SymSetOptions != nullptr) {
152
return g_pfn_SymSetOptions(arg);
157
DWORD WindowsDbgHelp::symGetOptions(void) {
158
WindowsDbgHelpEntry entry_guard;
159
if (g_pfn_SymGetOptions != nullptr) {
160
return g_pfn_SymGetOptions();
165
BOOL WindowsDbgHelp::symInitialize(HANDLE hProcess, PCTSTR UserSearchPath, BOOL fInvadeProcess) {
166
WindowsDbgHelpEntry entry_guard;
167
if (g_pfn_SymInitialize != nullptr) {
168
return g_pfn_SymInitialize(hProcess, UserSearchPath, fInvadeProcess);
173
BOOL WindowsDbgHelp::symGetSymFromAddr64(HANDLE hProcess, DWORD64 the_address,
174
PDWORD64 Displacement, PIMAGEHLP_SYMBOL64 Symbol) {
175
WindowsDbgHelpEntry entry_guard;
176
if (g_pfn_SymGetSymFromAddr64 != nullptr) {
177
return g_pfn_SymGetSymFromAddr64(hProcess, the_address, Displacement, Symbol);
182
DWORD WindowsDbgHelp::unDecorateSymbolName(const char* DecoratedName, char* UnDecoratedName,
183
DWORD UndecoratedLength, DWORD Flags) {
184
WindowsDbgHelpEntry entry_guard;
185
if (g_pfn_UnDecorateSymbolName != nullptr) {
186
return g_pfn_UnDecorateSymbolName(DecoratedName, UnDecoratedName, UndecoratedLength, Flags);
188
if (UnDecoratedName != nullptr && UndecoratedLength > 0) {
189
UnDecoratedName[0] = '\0';
194
BOOL WindowsDbgHelp::symSetSearchPath(HANDLE hProcess, PCTSTR SearchPath) {
195
WindowsDbgHelpEntry entry_guard;
196
if (g_pfn_SymSetSearchPath != nullptr) {
197
return g_pfn_SymSetSearchPath(hProcess, SearchPath);
202
BOOL WindowsDbgHelp::symGetSearchPath(HANDLE hProcess, PTSTR SearchPath, int SearchPathLength) {
203
WindowsDbgHelpEntry entry_guard;
204
if (g_pfn_SymGetSearchPath != nullptr) {
205
return g_pfn_SymGetSearchPath(hProcess, SearchPath, SearchPathLength);
210
BOOL WindowsDbgHelp::symRefreshModuleList(HANDLE hProcess) {
211
WindowsDbgHelpEntry entry_guard;
212
if (g_pfn_SymRefreshModuleList != nullptr) {
213
return g_pfn_SymRefreshModuleList(hProcess);
218
BOOL WindowsDbgHelp::stackWalk64(DWORD MachineType,
221
LPSTACKFRAME64 StackFrame,
222
PVOID ContextRecord) {
223
WindowsDbgHelpEntry entry_guard;
224
if (g_pfn_StackWalk64 != nullptr) {
225
return g_pfn_StackWalk64(MachineType, hProcess, hThread, StackFrame,
227
nullptr, // ReadMemoryRoutine
228
g_pfn_SymFunctionTableAccess64, // FunctionTableAccessRoutine,
229
g_pfn_SymGetModuleBase64, // GetModuleBaseRoutine
230
nullptr // TranslateAddressRoutine
236
PVOID WindowsDbgHelp::symFunctionTableAccess64(HANDLE hProcess, DWORD64 AddrBase) {
237
WindowsDbgHelpEntry entry_guard;
238
if (g_pfn_SymFunctionTableAccess64 != nullptr) {
239
return g_pfn_SymFunctionTableAccess64(hProcess, AddrBase);
244
DWORD64 WindowsDbgHelp::symGetModuleBase64(HANDLE hProcess, DWORD64 dwAddr) {
245
WindowsDbgHelpEntry entry_guard;
246
if (g_pfn_SymGetModuleBase64 != nullptr) {
247
return g_pfn_SymGetModuleBase64(hProcess, dwAddr);
252
BOOL WindowsDbgHelp::miniDumpWriteDump(HANDLE hProcess, DWORD ProcessId, HANDLE hFile,
253
MINIDUMP_TYPE DumpType, PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
254
PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
255
PMINIDUMP_CALLBACK_INFORMATION CallbackParam) {
256
WindowsDbgHelpEntry entry_guard;
257
if (g_pfn_MiniDumpWriteDump != nullptr) {
258
return g_pfn_MiniDumpWriteDump(hProcess, ProcessId, hFile, DumpType,
259
ExceptionParam, UserStreamParam, CallbackParam);
264
BOOL WindowsDbgHelp::symGetLineFromAddr64(HANDLE hProcess, DWORD64 dwAddr,
265
PDWORD pdwDisplacement, PIMAGEHLP_LINE64 Line) {
266
WindowsDbgHelpEntry entry_guard;
267
if (g_pfn_SymGetLineFromAddr64 != nullptr) {
268
return g_pfn_SymGetLineFromAddr64(hProcess, dwAddr, pdwDisplacement, Line);
273
// Print one liner describing state (if library loaded, which functions are
274
// missing - if any, and the dbhelp API version)
275
void WindowsDbgHelp::print_state_on(outputStream* st) {
276
// Note: We should not lock while printing, but this should be
277
// safe to do without lock anyway.
278
st->print("dbghelp: ");
280
if (g_state == state_uninitialized) {
281
st->print("uninitialized.");
282
} else if (g_state == state_error) {
283
st->print("loading error: %u", g_dll_load_error);
285
st->print("loaded successfully ");
287
// We may want to print dll file name here - which may be interesting for
288
// cases where more than one version exists on the system, e.g. with a
289
// debugging sdk separately installed. But we get the file name in the DLL
290
// section of the hs-err file too, so this may be redundant.
293
st->print("- version: %u.%u.%u",
294
g_version.MajorVersion, g_version.MinorVersion, g_version.Revision);
296
// Print any functions which failed to load.
298
st->print(" - missing functions: ");
300
#define CHECK_AND_PRINT_IF_NULL(functionname) \
301
if (g_pfn_##functionname == nullptr) { \
302
st->print("%s" #functionname, ((num_missing > 0) ? ", " : "")); \
306
FOR_ALL_FUNCTIONS(CHECK_AND_PRINT_IF_NULL)
308
if (num_missing == 0) {