VirtualBox

source: vbox/trunk/src/VBox/Runtime/tools/RTNtDbgHelp.cpp@ 82968

Last change on this file since 82968 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1/* $Id: RTNtDbgHelp.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - RTNtDbgHelp - Tool for working/exploring DbgHelp.dll.
4 */
5
6/*
7 * Copyright (C) 2013-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/win/windows.h>
32#include <Dbghelp.h>
33
34#include <iprt/alloca.h>
35#include <iprt/dir.h>
36#include <iprt/file.h>
37#include <iprt/getopt.h>
38#include <iprt/env.h>
39#include <iprt/initterm.h>
40#include <iprt/list.h>
41#include <iprt/mem.h>
42#include <iprt/message.h>
43#include <iprt/path.h>
44#include <iprt/stream.h>
45#include <iprt/string.h>
46#include <iprt/errcore.h>
47
48#include <iprt/win/lazy-dbghelp.h>
49
50#include <iprt/ldrlazy.h>
51
52
53/*********************************************************************************************************************************
54* Structures and Typedefs *
55*********************************************************************************************************************************/
56/**
57 * Debug module record.
58 *
59 * Used for dumping the whole context.
60 */
61typedef struct RTNTDBGHELPMOD
62{
63 /** The list bits. */
64 RTLISTNODE ListEntry;
65 /** The module address. */
66 uint64_t uModAddr;
67 /** Pointer to the name part of szFullName. */
68 char *pszName;
69 /** The module name. */
70 char szFullName[1];
71} RTNTDBGHELPMOD;
72/** Pointer to a debug module. */
73typedef RTNTDBGHELPMOD *PRTNTDBGHELPMOD;
74
75
76/*********************************************************************************************************************************
77* Global Variables *
78*********************************************************************************************************************************/
79/** Verbosity level. */
80static int g_iOptVerbose = 1;
81
82/** Fake process handle. */
83static HANDLE g_hFake = (HANDLE)0x1234567;
84/** Number of modules in the list. */
85static uint32_t g_cModules = 0;
86/** Module list. */
87static RTLISTANCHOR g_ModuleList;
88/** Set when initialized, clear until then. Lazy init on first operation. */
89static bool g_fInitialized = false;
90
91/** The current address register. */
92static uint64_t g_uCurAddress = 0;
93
94
95
96/**
97 * For debug/verbose output.
98 *
99 * @param iMin The minimum verbosity level for this message.
100 * @param pszFormat The format string.
101 * @param ... The arguments referenced in the format string.
102 */
103static void infoPrintf(int iMin, const char *pszFormat, ...)
104{
105 if (g_iOptVerbose >= iMin)
106 {
107 va_list va;
108 va_start(va, pszFormat);
109 RTPrintf("info: ");
110 RTPrintfV(pszFormat, va);
111 va_end(va);
112 }
113}
114
115static BOOL CALLBACK symDebugCallback64(HANDLE hProcess, ULONG uAction, ULONG64 ullData, ULONG64 ullUserCtx)
116{
117 NOREF(hProcess); NOREF(ullUserCtx);
118 switch (uAction)
119 {
120 case CBA_DEBUG_INFO:
121 {
122 const char *pszMsg = (const char *)(uintptr_t)ullData;
123 size_t cchMsg = strlen(pszMsg);
124 if (cchMsg > 0 && pszMsg[cchMsg - 1] == '\n')
125 RTPrintf("cba_debug_info: %s", pszMsg);
126 else
127 RTPrintf("cba_debug_info: %s\n", pszMsg);
128 return TRUE;
129 }
130
131 case CBA_DEFERRED_SYMBOL_LOAD_CANCEL:
132 return FALSE;
133
134 case CBA_EVENT:
135 return FALSE;
136
137 default:
138 RTPrintf("cba_???: uAction=%#x ullData=%#llx\n", uAction, ullData);
139 break;
140 }
141
142 return FALSE;
143}
144
145/**
146 * Lazy initialization.
147 * @returns Exit code with any relevant complaints printed.
148 */
149static RTEXITCODE ensureInitialized(void)
150{
151 if (!g_fInitialized)
152 {
153 if (!SymInitialize(g_hFake, NULL, FALSE))
154 return RTMsgErrorExit(RTEXITCODE_FAILURE, "SymInitialied failed: %u\n", GetLastError());
155 if (!SymRegisterCallback64(g_hFake, symDebugCallback64, 0))
156 return RTMsgErrorExit(RTEXITCODE_FAILURE, "SymRegisterCallback64 failed: %u\n", GetLastError());
157 g_fInitialized = true;
158 infoPrintf(2, "SymInitialized(,,)\n");
159 }
160 return RTEXITCODE_SUCCESS;
161}
162
163
164/**
165 * Loads the given module, the address is either automatic or a previously given
166 * one.
167 *
168 * @returns Exit code with any relevant complaints printed.
169 * @param pszFile The file to load.
170 */
171static RTEXITCODE loadModule(const char *pszFile)
172{
173 RTEXITCODE rcExit = ensureInitialized();
174 if (rcExit != RTEXITCODE_SUCCESS)
175 return rcExit;
176
177 uint64_t uModAddrReq = g_uCurAddress == 0 ? UINT64_C(0x1000000) * g_cModules : g_uCurAddress;
178 uint64_t uModAddrGot = SymLoadModuleEx(g_hFake, NULL /*hFile*/, pszFile, NULL /*pszModuleName*/,
179 uModAddrReq, 0, NULL /*pData*/, 0 /*fFlags*/);
180 if (uModAddrGot == 0)
181 return RTMsgErrorExit(RTEXITCODE_FAILURE, "SymLoadModuleEx failed: %u\n", GetLastError());
182
183 size_t cbFullName = strlen(pszFile) + 1;
184 PRTNTDBGHELPMOD pMod = (PRTNTDBGHELPMOD)RTMemAlloc(RT_UOFFSETOF_DYN(RTNTDBGHELPMOD, szFullName[cbFullName + 1]));
185 memcpy(pMod->szFullName, pszFile, cbFullName);
186 pMod->pszName = RTPathFilename(pMod->szFullName);
187 pMod->uModAddr = uModAddrGot;
188 RTListAppend(&g_ModuleList, &pMod->ListEntry);
189 infoPrintf(1, "%#018RX64 %s\n", pMod->uModAddr, pMod->pszName);
190
191 return RTEXITCODE_SUCCESS;
192}
193
194
195/**
196 * Translates SYM_TYPE to string.
197 *
198 * @returns String.
199 * @param enmType The symbol type value.
200 */
201static const char *symTypeName(SYM_TYPE enmType)
202{
203 switch (enmType)
204 {
205 case SymCoff: return "SymCoff";
206 case SymCv: return "SymCv";
207 case SymPdb: return "SymPdb";
208 case SymExport: return "SymExport";
209 case SymDeferred: return "SymDeferred";
210 case SymSym: return "SymSym";
211 case SymDia: return "SymDia";
212 case SymVirtual: return "SymVirtual";
213 default:
214 {
215 static char s_szBuf[32];
216 RTStrPrintf(s_szBuf, sizeof(s_szBuf), "Unknown-%#x", enmType);
217 return s_szBuf;
218 }
219 }
220}
221
222
223/**
224 * Symbol enumeration callback.
225 *
226 * @returns TRUE (continue enum).
227 * @param pSymInfo The symbol info.
228 * @param cbSymbol The symbol length (calculated).
229 * @param pvUser NULL.
230 */
231static BOOL CALLBACK dumpSymbolCallback(PSYMBOL_INFO pSymInfo, ULONG cbSymbol, PVOID pvUser)
232{
233 NOREF(pvUser);
234 RTPrintf(" %#018RX64 LB %#07x %s\n", pSymInfo->Address, cbSymbol, pSymInfo->Name);
235 return TRUE;
236}
237
238/**
239 * Dumps all info.
240 * @returns Exit code with any relevant complaints printed.
241 */
242static RTEXITCODE dumpAll(void)
243{
244 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
245 PRTNTDBGHELPMOD pMod;
246 RTListForEach(&g_ModuleList, pMod, RTNTDBGHELPMOD, ListEntry)
247 {
248 RTPrintf("*** %#018RX64 - %s ***\n", pMod->uModAddr, pMod->szFullName);
249
250 static const int8_t s_acbVariations[] = { 0, -4, -8, -12, -16, -20, -24, -28, -32, 4, 8, 12, 16, 20, 24, 28, 32 };
251 unsigned iVariation = 0;
252 union
253 {
254 IMAGEHLP_MODULE64 ModInfo;
255 uint8_t abPadding[sizeof(IMAGEHLP_MODULE64) + 64];
256 } u;
257
258 BOOL fRc;
259 do
260 {
261 RT_ZERO(u.ModInfo);
262 u.ModInfo.SizeOfStruct = sizeof(u.ModInfo) + s_acbVariations[iVariation++];
263 fRc = SymGetModuleInfo64(g_hFake, pMod->uModAddr, &u.ModInfo);
264 } while (!fRc && GetLastError() == ERROR_INVALID_PARAMETER && iVariation < RT_ELEMENTS(s_acbVariations));
265
266 if (fRc)
267 {
268 RTPrintf(" BaseOfImage = %#018llx\n", u.ModInfo.BaseOfImage);
269 RTPrintf(" ImageSize = %#010x\n", u.ModInfo.ImageSize);
270 RTPrintf(" TimeDateStamp = %#010x\n", u.ModInfo.TimeDateStamp);
271 RTPrintf(" CheckSum = %#010x\n", u.ModInfo.CheckSum);
272 RTPrintf(" NumSyms = %#010x (%u)\n", u.ModInfo.NumSyms, u.ModInfo.NumSyms);
273 RTPrintf(" SymType = %s\n", symTypeName(u.ModInfo.SymType));
274 RTPrintf(" ModuleName = %.32s\n", u.ModInfo.ModuleName);
275 RTPrintf(" ImageName = %.256s\n", u.ModInfo.ImageName);
276 RTPrintf(" LoadedImageName = %.256s\n", u.ModInfo.LoadedImageName);
277 RTPrintf(" LoadedPdbName = %.256s\n", u.ModInfo.LoadedPdbName);
278 RTPrintf(" CVSig = %#010x\n", u.ModInfo.CVSig);
279 /** @todo CVData. */
280 RTPrintf(" PdbSig = %#010x\n", u.ModInfo.PdbSig);
281 RTPrintf(" PdbSig70 = %RTuuid\n", &u.ModInfo.PdbSig70);
282 RTPrintf(" PdbAge = %#010x\n", u.ModInfo.PdbAge);
283 RTPrintf(" PdbUnmatched = %RTbool\n", u.ModInfo.PdbUnmatched);
284 RTPrintf(" DbgUnmatched = %RTbool\n", u.ModInfo.DbgUnmatched);
285 RTPrintf(" LineNumbers = %RTbool\n", u.ModInfo.LineNumbers);
286 RTPrintf(" GlobalSymbols = %RTbool\n", u.ModInfo.GlobalSymbols);
287 RTPrintf(" TypeInfo = %RTbool\n", u.ModInfo.TypeInfo);
288 RTPrintf(" SourceIndexed = %RTbool\n", u.ModInfo.SourceIndexed);
289 RTPrintf(" Publics = %RTbool\n", u.ModInfo.Publics);
290 }
291 else
292 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymGetModuleInfo64 failed: %u\n", GetLastError());
293
294 if (!SymEnumSymbols(g_hFake, pMod->uModAddr, NULL, dumpSymbolCallback, NULL))
295 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymEnumSymbols failed: %u\n", GetLastError());
296
297 }
298 return rcExit;
299}
300
301
302int main(int argc, char **argv)
303{
304 int rc = RTR3InitExe(argc, &argv, 0 /*fFlags*/);
305 if (RT_FAILURE(rc))
306 return RTMsgInitFailure(rc);
307
308 RTListInit(&g_ModuleList);
309
310 /*
311 * Parse options.
312 */
313 static const RTGETOPTDEF s_aOptions[] =
314 {
315 { "--dump-all", 'd', RTGETOPT_REQ_NOTHING },
316 { "--load", 'l', RTGETOPT_REQ_STRING },
317 { "--set-address", 'a', RTGETOPT_REQ_UINT64 },
318#define OPT_SET_DEBUG_INFO 0x1000
319 { "--set-debug-info", OPT_SET_DEBUG_INFO, RTGETOPT_REQ_NOTHING },
320 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
321 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
322 };
323
324 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
325 //const char *pszOutput = "-";
326
327 int ch;
328 RTGETOPTUNION ValueUnion;
329 RTGETOPTSTATE GetState;
330 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
331 RTGETOPTINIT_FLAGS_OPTS_FIRST);
332 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
333 {
334 switch (ch)
335 {
336 case 'v':
337 g_iOptVerbose++;
338 break;
339
340 case 'q':
341 g_iOptVerbose++;
342 break;
343
344 case 'l':
345 rcExit = loadModule(ValueUnion.psz);
346 break;
347
348 case 'a':
349 g_uCurAddress = ValueUnion.u64;
350 break;
351
352 case 'd':
353 rcExit = dumpAll();
354 break;
355
356 case OPT_SET_DEBUG_INFO:
357 rcExit = ensureInitialized();
358 if (rcExit == RTEXITCODE_SUCCESS && !SymSetOptions(SymGetOptions() | SYMOPT_DEBUG))
359 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymSetOptions failed: %u\n", GetLastError());
360 break;
361
362
363 case 'V':
364 RTPrintf("$Revision: 82968 $");
365 break;
366
367 case 'h':
368 RTPrintf("usage: %s [-v|--verbose] [-q|--quiet] [--set-debug-info] [-a <addr>] [-l <file>] [-d] [...]\n"
369 " or: %s [-V|--version]\n"
370 " or: %s [-h|--help]\n",
371 argv[0], argv[0], argv[0]);
372 return RTEXITCODE_SUCCESS;
373
374 case VINF_GETOPT_NOT_OPTION:
375 default:
376 return RTGetOptPrintError(ch, &ValueUnion);
377 }
378 if (rcExit != RTEXITCODE_SUCCESS)
379 break;
380 }
381 return rcExit;
382}
383
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette