VirtualBox

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

Last change on this file since 60067 was 56290, checked in by vboxsync, 10 years ago

IPRT: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.8 KB
Line 
1/* $Id: RTNtDbgHelp.cpp 56290 2015-06-09 14:01:31Z vboxsync $ */
2/** @file
3 * IPRT - RTNtDbgHelp - Tool for working/exploring DbgHelp.dll.
4 */
5
6/*
7 * Copyright (C) 2013-2015 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 <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/err.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_OFFSETOF(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 }
214 static char s_szBuf[32];
215 RTStrPrintf(s_szBuf, sizeof(s_szBuf), "Unknown-%#x", enmType);
216 return s_szBuf;
217}
218
219
220/**
221 * Symbol enumeration callback.
222 *
223 * @returns TRUE (continue enum).
224 * @param pSymInfo The symbol info.
225 * @param cbSymbol The symbol length (calculated).
226 * @param pvUser NULL.
227 */
228static BOOL CALLBACK dumpSymbolCallback(PSYMBOL_INFO pSymInfo, ULONG cbSymbol, PVOID pvUser)
229{
230 NOREF(pvUser);
231 RTPrintf(" %#018RX64 LB %#07x %s\n", pSymInfo->Address, cbSymbol, pSymInfo->Name);
232 return TRUE;
233}
234
235/**
236 * Dumps all info.
237 * @returns Exit code with any relevant complaints printed.
238 */
239static RTEXITCODE dumpAll(void)
240{
241 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
242 PRTNTDBGHELPMOD pMod;
243 RTListForEach(&g_ModuleList, pMod, RTNTDBGHELPMOD, ListEntry)
244 {
245 RTPrintf("*** %#018RX64 - %s ***\n", pMod->uModAddr, pMod->szFullName);
246
247 static const int8_t s_acbVariations[] = { 0, -4, -8, -12, -16, -20, -24, -28, -32, 4, 8, 12, 16, 20, 24, 28, 32 };
248 unsigned iVariation = 0;
249 union
250 {
251 IMAGEHLP_MODULE64 ModInfo;
252 uint8_t abPadding[sizeof(IMAGEHLP_MODULE64) + 64];
253 } u;
254
255 BOOL fRc;
256 do
257 {
258 RT_ZERO(u.ModInfo);
259 u.ModInfo.SizeOfStruct = sizeof(u.ModInfo) + s_acbVariations[iVariation++];
260 fRc = SymGetModuleInfo64(g_hFake, pMod->uModAddr, &u.ModInfo);
261 } while (!fRc && GetLastError() == ERROR_INVALID_PARAMETER && iVariation < RT_ELEMENTS(s_acbVariations));
262
263 if (fRc)
264 {
265 RTPrintf(" BaseOfImage = %#018llx\n", u.ModInfo.BaseOfImage);
266 RTPrintf(" ImageSize = %#010x\n", u.ModInfo.ImageSize);
267 RTPrintf(" TimeDateStamp = %#010x\n", u.ModInfo.TimeDateStamp);
268 RTPrintf(" CheckSum = %#010x\n", u.ModInfo.CheckSum);
269 RTPrintf(" NumSyms = %#010x (%u)\n", u.ModInfo.NumSyms, u.ModInfo.NumSyms);
270 RTPrintf(" SymType = %s\n", symTypeName(u.ModInfo.SymType));
271 RTPrintf(" ModuleName = %.32s\n", u.ModInfo.ModuleName);
272 RTPrintf(" ImageName = %.256s\n", u.ModInfo.ImageName);
273 RTPrintf(" LoadedImageName = %.256s\n", u.ModInfo.LoadedImageName);
274 RTPrintf(" LoadedPdbName = %.256s\n", u.ModInfo.LoadedPdbName);
275 RTPrintf(" CVSig = %#010x\n", u.ModInfo.CVSig);
276 /** @todo CVData. */
277 RTPrintf(" PdbSig = %#010x\n", u.ModInfo.PdbSig);
278 RTPrintf(" PdbSig70 = %RTuuid\n", &u.ModInfo.PdbSig70);
279 RTPrintf(" PdbAge = %#010x\n", u.ModInfo.PdbAge);
280 RTPrintf(" PdbUnmatched = %RTbool\n", u.ModInfo.PdbUnmatched);
281 RTPrintf(" DbgUnmatched = %RTbool\n", u.ModInfo.DbgUnmatched);
282 RTPrintf(" LineNumbers = %RTbool\n", u.ModInfo.LineNumbers);
283 RTPrintf(" GlobalSymbols = %RTbool\n", u.ModInfo.GlobalSymbols);
284 RTPrintf(" TypeInfo = %RTbool\n", u.ModInfo.TypeInfo);
285 RTPrintf(" SourceIndexed = %RTbool\n", u.ModInfo.SourceIndexed);
286 RTPrintf(" Publics = %RTbool\n", u.ModInfo.Publics);
287 }
288 else
289 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymGetModuleInfo64 failed: %u\n", GetLastError());
290
291 if (!SymEnumSymbols(g_hFake, pMod->uModAddr, NULL, dumpSymbolCallback, NULL))
292 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymEnumSymbols failed: %u\n", GetLastError());
293
294 }
295 return rcExit;
296}
297
298
299int main(int argc, char **argv)
300{
301 int rc = RTR3InitExe(argc, &argv, 0 /*fFlags*/);
302 if (RT_FAILURE(rc))
303 return RTMsgInitFailure(rc);
304
305 RTListInit(&g_ModuleList);
306
307 /*
308 * Parse options.
309 */
310 static const RTGETOPTDEF s_aOptions[] =
311 {
312 { "--dump-all", 'd', RTGETOPT_REQ_NOTHING },
313 { "--load", 'l', RTGETOPT_REQ_STRING },
314 { "--set-address", 'a', RTGETOPT_REQ_UINT64 },
315#define OPT_SET_DEBUG_INFO 0x1000
316 { "--set-debug-info", OPT_SET_DEBUG_INFO, RTGETOPT_REQ_NOTHING },
317 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
318 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
319 };
320
321 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
322 const char *pszOutput = "-";
323
324 int ch;
325 RTGETOPTUNION ValueUnion;
326 RTGETOPTSTATE GetState;
327 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
328 RTGETOPTINIT_FLAGS_OPTS_FIRST);
329 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
330 {
331 switch (ch)
332 {
333 case 'v':
334 g_iOptVerbose++;
335 break;
336
337 case 'q':
338 g_iOptVerbose++;
339 break;
340
341 case 'l':
342 rcExit = loadModule(ValueUnion.psz);
343 break;
344
345 case 'a':
346 g_uCurAddress = ValueUnion.u64;
347 break;
348
349 case 'd':
350 rcExit = dumpAll();
351 break;
352
353 case OPT_SET_DEBUG_INFO:
354 rcExit = ensureInitialized();
355 if (rcExit == RTEXITCODE_SUCCESS && !SymSetOptions(SymGetOptions() | SYMOPT_DEBUG))
356 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymSetOptions failed: %u\n", GetLastError());
357 break;
358
359
360 case 'V':
361 RTPrintf("$Revision: 56290 $");
362 break;
363
364 case 'h':
365 RTPrintf("usage: %s [-v|--verbose] [-q|--quiet] [--set-debug-info] [-a <addr>] [-l <file>] [-d] [...]\n"
366 " or: %s [-V|--version]\n"
367 " or: %s [-h|--help]\n",
368 argv[0], argv[0], argv[0]);
369 return RTEXITCODE_SUCCESS;
370
371 case VINF_GETOPT_NOT_OPTION:
372 default:
373 return RTGetOptPrintError(ch, &ValueUnion);
374 }
375 if (rcExit != RTEXITCODE_SUCCESS)
376 break;
377 }
378 return rcExit;
379}
380
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