VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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