VirtualBox

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

Last change on this file since 46072 was 45929, checked in by vboxsync, 12 years ago

RTNtDbgHelp: Tool for exploring debug info and/or dbghelp.dll apis.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1/* $Id: RTNtDbgHelp.cpp 45929 2013-05-07 08:34:15Z vboxsync $ */
2/** @file
3 * IPRT - RTNtDbgHelp - Tool for working/exploring DbgHelp.dll.
4 */
5
6/*
7 * Copyright (C) 2013 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, "%#018x %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(" %#018x 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("*** %#018x - %s ***\n", pMod->uModAddr, pMod->szFullName);
246
247 IMAGEHLP_MODULE64 ModInfo;
248 RT_ZERO(ModInfo);
249 ModInfo.SizeOfStruct = sizeof(ModInfo);
250 if (SymGetModuleInfo64(g_hFake, pMod->uModAddr, &ModInfo))
251 {
252 RTPrintf(" BaseOfImage = %#018llx\n", ModInfo.BaseOfImage);
253 RTPrintf(" ImageSize = %#010x\n", ModInfo.ImageSize);
254 RTPrintf(" TimeDateStamp = %#010x\n", ModInfo.TimeDateStamp);
255 RTPrintf(" CheckSum = %#010x\n", ModInfo.CheckSum);
256 RTPrintf(" NumSyms = %#010x (%u)\n", ModInfo.NumSyms, ModInfo.NumSyms);
257 RTPrintf(" SymType = %s\n", symTypeName(ModInfo.SymType));
258 RTPrintf(" ModuleName = %.32s\n", ModInfo.ModuleName);
259 RTPrintf(" ImageName = %.256s\n", ModInfo.ImageName);
260 RTPrintf(" LoadedImageName = %.256s\n", ModInfo.LoadedImageName);
261 RTPrintf(" LoadedPdbName = %.256s\n", ModInfo.LoadedPdbName);
262 RTPrintf(" CVSig = %#010x\n", ModInfo.CVSig);
263 /** @todo CVData. */
264 RTPrintf(" PdbSig = %#010x\n", ModInfo.PdbSig);
265 RTPrintf(" PdbSig70 = %RTuuid\n", &ModInfo.PdbSig70);
266 RTPrintf(" PdbSig = %#010x\n", ModInfo.PdbAge);
267 RTPrintf(" PdbUnmatched = %RTbool\n", ModInfo.PdbUnmatched);
268 RTPrintf(" DbgUnmatched = %RTbool\n", ModInfo.DbgUnmatched);
269 RTPrintf(" LineNumbers = %RTbool\n", ModInfo.LineNumbers);
270 RTPrintf(" GlobalSymbols = %RTbool\n", ModInfo.GlobalSymbols);
271 RTPrintf(" TypeInfo = %RTbool\n", ModInfo.TypeInfo);
272 RTPrintf(" SourceIndexed = %RTbool\n", ModInfo.SourceIndexed);
273 RTPrintf(" Publics = %RTbool\n", ModInfo.Publics);
274 }
275 else
276 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymGetModuleInfo64 failed: %u\n", GetLastError());
277
278 if (!SymEnumSymbols(g_hFake, pMod->uModAddr, NULL, dumpSymbolCallback, NULL))
279 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymEnumSymbols failed: %u\n", GetLastError());
280
281 }
282 return rcExit;
283}
284
285
286int main(int argc, char **argv)
287{
288 int rc = RTR3InitExe(argc, &argv, 0 /*fFlags*/);
289 if (RT_FAILURE(rc))
290 return RTMsgInitFailure(rc);
291
292 RTListInit(&g_ModuleList);
293
294 /*
295 * Parse options.
296 */
297 static const RTGETOPTDEF s_aOptions[] =
298 {
299 { "--dump-all", 'd', RTGETOPT_REQ_NOTHING },
300 { "--load", 'l', RTGETOPT_REQ_STRING },
301 { "--set-address", 'a', RTGETOPT_REQ_UINT64 },
302#define OPT_SET_DEBUG_INFO 0x1000
303 { "--set-debug-info", OPT_SET_DEBUG_INFO, RTGETOPT_REQ_NOTHING },
304 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
305 { "--quiet", 'q', RTGETOPT_REQ_NOTHING },
306 };
307
308 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
309 const char *pszOutput = "-";
310
311 int ch;
312 RTGETOPTUNION ValueUnion;
313 RTGETOPTSTATE GetState;
314 RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
315 RTGETOPTINIT_FLAGS_OPTS_FIRST);
316 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
317 {
318 switch (ch)
319 {
320 case 'v':
321 g_iOptVerbose++;
322 break;
323
324 case 'q':
325 g_iOptVerbose++;
326 break;
327
328 case 'l':
329 rcExit = loadModule(ValueUnion.psz);
330 break;
331
332 case 'a':
333 g_uCurAddress = ValueUnion.u64;
334 break;
335
336 case 'd':
337 rcExit = dumpAll();
338 break;
339
340 case OPT_SET_DEBUG_INFO:
341 rcExit = ensureInitialized();
342 if (rcExit == RTEXITCODE_SUCCESS && !SymSetOptions(SymGetOptions() | SYMOPT_DEBUG))
343 rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "SymSetOptions failed: %u\n", GetLastError());
344 break;
345
346
347 case 'V':
348 RTPrintf("$Revision: 45929 $");
349 break;
350
351 case 'h':
352 RTPrintf("usage: %s [-v|--verbose] [-q|--quiet] [-f|--force] [-o|--output <file.h>] <dir1|pdb1> [...]\n"
353 " or: %s [-V|--version]\n"
354 " or: %s [-h|--help]\n",
355 argv[0], argv[0], argv[0]);
356 return RTEXITCODE_SUCCESS;
357
358 case VINF_GETOPT_NOT_OPTION:
359 default:
360 return RTGetOptPrintError(ch, &ValueUnion);
361 }
362 if (rcExit != RTEXITCODE_SUCCESS)
363 break;
364 }
365 return rcExit;
366}
367
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