jdk

Форк
0
/
windbghelp.cpp 
313 строк · 11.0 Кб
1
/*
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.
4
 *
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.
8
 *
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).
14
 *
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.
18
 *
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
21
 * questions.
22
 *
23
 */
24

25
#include "precompiled.hpp"
26
#include "utilities/ostream.hpp"
27
#include "windbghelp.hpp"
28

29
#include <windows.h>
30

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,
40
                                        HANDLE hProcess,
41
                                        HANDLE hThread,
42
                                        LPSTACKFRAME64 StackFrame,
43
                                        PVOID ContextRecord,
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);
57

58
// Add functions as needed.
59
#define FOR_ALL_FUNCTIONS(DO) \
60
 DO(ImagehlpApiVersion) \
61
 DO(SymGetOptions) \
62
 DO(SymSetOptions) \
63
 DO(SymInitialize) \
64
 DO(SymGetSymFromAddr64) \
65
 DO(UnDecorateSymbolName) \
66
 DO(SymSetSearchPath) \
67
 DO(SymGetSearchPath) \
68
 DO(StackWalk64) \
69
 DO(SymFunctionTableAccess64) \
70
 DO(SymGetModuleBase64) \
71
 DO(MiniDumpWriteDump) \
72
 DO(SymGetLineFromAddr64) \
73
 DO(SymRefreshModuleList)
74

75

76
#define DECLARE_FUNCTION_POINTER(functionname) \
77
static pfn_##functionname g_pfn_##functionname;
78

79
FOR_ALL_FUNCTIONS(DECLARE_FUNCTION_POINTER)
80

81

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 };
85

86
static enum {
87
  state_uninitialized = 0,
88
  state_ready = 1,
89
  state_error = 2
90
} g_state = state_uninitialized;
91

92
static void initialize() {
93

94
  assert(g_state == state_uninitialized, "wrong sequence");
95
  g_state = state_error;
96

97
  g_dll_handle = ::LoadLibrary("DBGHELP.DLL");
98
  if (g_dll_handle == nullptr) {
99
    g_dll_load_error = ::GetLastError();
100
  } else {
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;
106

107
#define DO_RESOLVE(functionname) \
108
      g_pfn_##functionname = (pfn_##functionname) ::GetProcAddress(g_dll_handle, #functionname);
109

110
    FOR_ALL_FUNCTIONS(DO_RESOLVE)
111

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));
116
    }
117
  }
118

119
}
120

121

122
///////////////////// External functions //////////////////////////
123

124
// All outside facing functions are synchronized. Also, we run
125
// initialization on first touch.
126

127
static CRITICAL_SECTION g_cs;
128

129
namespace { // Do not export.
130
  class WindowsDbgHelpEntry {
131
   public:
132
    WindowsDbgHelpEntry() {
133
      ::EnterCriticalSection(&g_cs);
134
      if (g_state == state_uninitialized) {
135
        initialize();
136
      }
137
    }
138
    ~WindowsDbgHelpEntry() {
139
      ::LeaveCriticalSection(&g_cs);
140
    }
141
  };
142
}
143

144
// Called at DLL_PROCESS_ATTACH.
145
void WindowsDbgHelp::pre_initialize() {
146
  ::InitializeCriticalSection(&g_cs);
147
}
148

149
DWORD WindowsDbgHelp::symSetOptions(DWORD arg) {
150
  WindowsDbgHelpEntry entry_guard;
151
  if (g_pfn_SymSetOptions != nullptr) {
152
    return g_pfn_SymSetOptions(arg);
153
  }
154
  return 0;
155
}
156

157
DWORD WindowsDbgHelp::symGetOptions(void) {
158
  WindowsDbgHelpEntry entry_guard;
159
  if (g_pfn_SymGetOptions != nullptr) {
160
    return g_pfn_SymGetOptions();
161
  }
162
  return 0;
163
}
164

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);
169
  }
170
  return FALSE;
171
}
172

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);
178
  }
179
  return FALSE;
180
}
181

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);
187
  }
188
  if (UnDecoratedName != nullptr && UndecoratedLength > 0) {
189
    UnDecoratedName[0] = '\0';
190
  }
191
  return 0;
192
}
193

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);
198
  }
199
  return FALSE;
200
}
201

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);
206
  }
207
  return FALSE;
208
}
209

210
BOOL WindowsDbgHelp::symRefreshModuleList(HANDLE hProcess) {
211
  WindowsDbgHelpEntry entry_guard;
212
  if (g_pfn_SymRefreshModuleList != nullptr) {
213
    return g_pfn_SymRefreshModuleList(hProcess);
214
  }
215
  return FALSE;
216
}
217

218
BOOL WindowsDbgHelp::stackWalk64(DWORD MachineType,
219
                                 HANDLE hProcess,
220
                                 HANDLE hThread,
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,
226
                             ContextRecord,
227
                             nullptr, // ReadMemoryRoutine
228
                             g_pfn_SymFunctionTableAccess64, // FunctionTableAccessRoutine,
229
                             g_pfn_SymGetModuleBase64, // GetModuleBaseRoutine
230
                             nullptr // TranslateAddressRoutine
231
                             );
232
  }
233
  return FALSE;
234
}
235

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);
240
  }
241
  return nullptr;
242
}
243

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);
248
  }
249
  return 0;
250
}
251

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);
260
  }
261
  return FALSE;
262
}
263

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);
269
  }
270
  return FALSE;
271
}
272

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: ");
279

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);
284
  } else {
285
    st->print("loaded successfully ");
286

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.
291

292
    // Print version.
293
    st->print("- version: %u.%u.%u",
294
              g_version.MajorVersion, g_version.MinorVersion, g_version.Revision);
295

296
    // Print any functions which failed to load.
297
    int num_missing = 0;
298
    st->print(" - missing functions: ");
299

300
    #define CHECK_AND_PRINT_IF_NULL(functionname) \
301
    if (g_pfn_##functionname == nullptr) { \
302
      st->print("%s" #functionname, ((num_missing > 0) ? ", " : "")); \
303
      num_missing ++; \
304
    }
305

306
    FOR_ALL_FUNCTIONS(CHECK_AND_PRINT_IF_NULL)
307

308
    if (num_missing == 0) {
309
      st->print("none");
310
    }
311
  }
312
  st->cr();
313
}
314

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.