VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmoddbghelp.cpp@ 46071

Last change on this file since 46071 was 46048, checked in by vboxsync, 12 years ago

Added a RTDbgMod reader that employs DbgHelp.dll.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.3 KB
Line 
1/* $Id: dbgmoddbghelp.cpp 46048 2013-05-14 07:44:30Z vboxsync $ */
2/** @file
3 * IPRT - Debug Info Reader Using DbgHelp.dll if Present.
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#define LOG_GROUP RTLOGGROUP_DBG
32#include <iprt/dbg.h>
33#include "internal/iprt.h"
34
35#include <iprt/asm.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/list.h>
39#include <iprt/log.h>
40#include <iprt/mem.h>
41#include <iprt/path.h>
42#include <iprt/string.h>
43#include "internal/dbgmod.h"
44
45#include <Windows.h>
46#include <Dbghelp.h>
47#include <iprt/win/lazy-dbghelp.h>
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/** For passing arguments to DbgHelp.dll callback. */
54typedef struct RTDBGMODBGHELPARGS
55{
56 RTDBGMOD hCnt;
57 PRTDBGMODINT pMod;
58 uint64_t uModAddr;
59
60 /** UTF-8 version of the previous file name. */
61 char *pszPrev;
62 /** Copy of the previous file name. */
63 PRTUTF16 pwszPrev;
64 /** Number of bytes pwszPrev points to. */
65 size_t cbPrevUtf16Alloc;
66} RTDBGMODBGHELPARGS;
67
68
69
70/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
71static DECLCALLBACK(int) rtDbgModDbgHelp_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
72 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
73{
74 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
75 return RTDbgModLineByAddr(hCnt, iSeg, off, poffDisp, pLineInfo);
76}
77
78
79/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
80static DECLCALLBACK(int) rtDbgModDbgHelp_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
81{
82 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
83 return RTDbgModLineByOrdinal(hCnt, iOrdinal, pLineInfo);
84}
85
86
87/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
88static DECLCALLBACK(uint32_t) rtDbgModDbgHelp_LineCount(PRTDBGMODINT pMod)
89{
90 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
91 return RTDbgModLineCount(hCnt);
92}
93
94
95/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
96static DECLCALLBACK(int) rtDbgModDbgHelp_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
97 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
98{
99 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
100 Assert(!pszFile[cchFile]); NOREF(cchFile);
101 return RTDbgModLineAdd(hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
102}
103
104
105/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
106static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
107 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
108{
109 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
110 return RTDbgModSymbolByAddr(hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
111}
112
113
114/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
115static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
116 PRTDBGSYMBOL pSymInfo)
117{
118 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
119 Assert(!pszSymbol[cchSymbol]);
120 return RTDbgModSymbolByName(hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
121}
122
123
124/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
125static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
126{
127 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
128 return RTDbgModSymbolByOrdinal(hCnt, iOrdinal, pSymInfo);
129}
130
131
132/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
133static DECLCALLBACK(uint32_t) rtDbgModDbgHelp_SymbolCount(PRTDBGMODINT pMod)
134{
135 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
136 return RTDbgModSymbolCount(hCnt);
137}
138
139
140/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
141static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
142 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
143 uint32_t *piOrdinal)
144{
145 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
146 Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
147 return RTDbgModSymbolAdd(hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
148}
149
150
151/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
152static DECLCALLBACK(int) rtDbgModDbgHelp_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
153{
154 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
155 return RTDbgModSegmentByIndex(hCnt, iSeg, pSegInfo);
156}
157
158
159/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
160static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDbgHelp_SegmentCount(PRTDBGMODINT pMod)
161{
162 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
163 return RTDbgModSegmentCount(hCnt);
164}
165
166
167/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
168static DECLCALLBACK(int) rtDbgModDbgHelp_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
169 uint32_t fFlags, PRTDBGSEGIDX piSeg)
170{
171 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
172 Assert(!pszName[cchName]); NOREF(cchName);
173 return RTDbgModSegmentAdd(hCnt, uRva, cb, pszName, fFlags, piSeg);
174}
175
176
177/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
178static DECLCALLBACK(RTUINTPTR) rtDbgModDbgHelp_ImageSize(PRTDBGMODINT pMod)
179{
180 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
181 RTUINTPTR cb1 = RTDbgModImageSize(hCnt);
182 RTUINTPTR cb2 = pMod->pImgVt->pfnImageSize(pMod);
183 return RT_MAX(cb1, cb2);
184}
185
186
187/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
188static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDbgHelp_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
189{
190 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
191 return RTDbgModRvaToSegOff(hCnt, uRva, poffSeg);
192}
193
194
195/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
196static DECLCALLBACK(int) rtDbgModDbgHelp_Close(PRTDBGMODINT pMod)
197{
198 RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;;
199 RTDbgModRelease(hCnt);
200 pMod->pvDbgPriv = NULL;
201 return VINF_SUCCESS;
202}
203
204
205/**
206 * SymEnumLinesW callback that adds a line number to the container.
207 *
208 * @returns TRUE, FALSE if we're out of memory.
209 * @param pLineInfo Line number information.
210 * @param pvUser Pointer to a RTDBGMODBGHELPARGS structure.
211 */
212static BOOL CALLBACK rtDbgModDbgHelpCopyLineNumberCallback(PSRCCODEINFOW pLineInfo, PVOID pvUser)
213{
214 RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
215
216 /*
217 * To save having to call RTUtf16ToUtf8 every time, we keep a copy of the
218 * previous file name both as UTF-8 and UTF-16.
219 */
220 /** @todo we could combine RTUtf16Len and memcmp... */
221 size_t cbLen = (RTUtf16Len(pLineInfo->FileName) + 1) * sizeof(RTUTF16);
222 if ( !pArgs->pwszPrev
223 || memcmp(pArgs->pwszPrev, pLineInfo->FileName, cbLen) )
224 {
225 if (pArgs->cbPrevUtf16Alloc >= cbLen)
226 memcpy(pArgs->pwszPrev, pLineInfo->FileName, cbLen);
227 else
228 {
229 RTMemFree(pArgs->pwszPrev);
230 pArgs->cbPrevUtf16Alloc = cbLen;
231 pArgs->pwszPrev = (PRTUTF16)RTMemDupEx(pLineInfo->FileName, cbLen, pArgs->cbPrevUtf16Alloc - cbLen);
232 if (!pArgs->pwszPrev)
233 pArgs->cbPrevUtf16Alloc = 0;
234 }
235
236 RTStrFree(pArgs->pszPrev);
237 pArgs->pszPrev = NULL;
238 int rc = RTUtf16ToUtf8(pLineInfo->FileName, &pArgs->pszPrev);
239 if (RT_FAILURE(rc))
240 {
241 SetLastError(ERROR_OUTOFMEMORY);
242 Log(("rtDbgModDbgHelpCopyLineNumberCallback: Out of memory\n"));
243 return FALSE;
244 }
245 }
246
247 /*
248 * Add the line number to the container.
249 */
250 int rc = RTDbgModLineAdd(pArgs->hCnt, pArgs->pszPrev, pLineInfo->LineNumber,
251 RTDBGSEGIDX_RVA, pLineInfo->Address - pArgs->uModAddr, NULL);
252 Log((" %#018x %05u %s [%Rrc]\n", pLineInfo->Address, pLineInfo->LineNumber, rc));
253 NOREF(rc);
254
255 return TRUE;
256}
257
258
259/**
260 * Copies the line numbers into the container.
261 *
262 * @returns IPRT status code.
263 * @param pMod The debug module.
264 * @param hCnt The container that will keep the symbols.
265 * @param hFake The fake process handle.
266 * @param uModAddr The module load address.
267 */
268static int rtDbgModDbgHelpCopyLineNumbers(PRTDBGMODINT pMod, RTDBGMOD hCnt, HANDLE hFake, uint64_t uModAddr)
269{
270 RTDBGMODBGHELPARGS Args;
271 Args.hCnt = hCnt;
272 Args.pMod = pMod;
273 Args.uModAddr = uModAddr;
274 Args.pszPrev = NULL;
275 Args.pwszPrev = NULL;
276 Args.cbPrevUtf16Alloc = 0;
277
278 int rc;
279 if (SymEnumLinesW(hFake, uModAddr, NULL /*pszObj*/, NULL /*pszFile*/, rtDbgModDbgHelpCopyLineNumberCallback, &Args))
280 rc = VINF_SUCCESS;
281 else
282 {
283 rc = RTErrConvertFromWin32(GetLastError());
284 Log(("Line number enum: %Rrc (%u)\n", rc, GetLastError()));
285 if (rc == VERR_NOT_SUPPORTED)
286 rc = VINF_SUCCESS;
287 }
288
289 RTStrFree(Args.pszPrev);
290 RTMemFree(Args.pwszPrev);
291 return rc;
292}
293
294
295/**
296 * SymEnumSymbols callback that adds a symbol to the container.
297 *
298 * @returns TRUE
299 * @param pSymInfo The symbol information.
300 * @param cbSymbol The symbol size (estimated).
301 * @param pvUser Pointer to a RTDBGMODBGHELPARGS structure.
302 */
303static BOOL CALLBACK rtDbgModDbgHelpCopySymbolsCallback(PSYMBOL_INFO pSymInfo, ULONG cbSymbol, PVOID pvUser)
304{
305 RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
306
307 int rc = RTDbgModSymbolAdd(pArgs->hCnt, pSymInfo->Name, RTDBGSEGIDX_RVA,
308 pSymInfo->Address - pArgs->uModAddr, cbSymbol, 0, NULL);
309 Log((" %#018x LB %#07x %s [%Rrc]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name, rc));
310 NOREF(rc);
311
312 return TRUE;
313}
314
315
316/**
317 * Copies the symbols into the container.
318 *
319 * @returns IPRT status code.
320 * @param pMod The debug module.
321 * @param hCnt The container that will keep the symbols.
322 * @param hFake The fake process handle.
323 * @param uModAddr The module load address.
324 */
325static int rtDbgModDbgHelpCopySymbols(PRTDBGMODINT pMod, RTDBGMOD hCnt, HANDLE hFake, uint64_t uModAddr)
326{
327 RTDBGMODBGHELPARGS Args;
328 Args.hCnt = hCnt;
329 Args.pMod = pMod;
330 Args.uModAddr = uModAddr;
331 int rc;
332 if (SymEnumSymbols(hFake, uModAddr, NULL, rtDbgModDbgHelpCopySymbolsCallback, &Args))
333 rc = VINF_SUCCESS;
334 else
335 {
336 rc = RTErrConvertFromWin32(GetLastError());
337 Log(("SymEnumSymbols: %Rrc (%u)\n", rc, GetLastError()));
338 }
339 return rc;
340}
341
342
343/** @callback_method_impl{FNRTLDRENUMSEGS, Copies the PE segments over into
344 * the container.} */
345static DECLCALLBACK(int) rtDbgModDbgHelpAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
346{
347 RTDBGMOD hCnt = (RTDBGMOD)pvUser;
348
349 Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
350 pSeg->cchName, pSeg->pchName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
351 NOREF(hLdrMod);
352 char *pszName = (char *)pSeg->pchName;
353 if (pszName[pSeg->cchName])
354 {
355 pszName = (char *)alloca(pSeg->cchName + 1);
356 memcpy(pszName, pSeg->pchName, pSeg->cchName);
357 pszName[pSeg->cchName] = '\0';
358 }
359
360 RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
361 return RTDbgModSegmentAdd(hCnt, pSeg->RVA, cb, pszName, 0 /*fFlags*/, NULL);
362}
363
364
365/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
366static DECLCALLBACK(int) rtDbgModDbgHelp_TryOpen(PRTDBGMODINT pMod)
367{
368 /*
369 * Currently only support external files with a executable already present.
370 */
371 if (!pMod->pszDbgFile)
372 return VERR_DBG_NO_MATCHING_INTERPRETER;
373 if (!pMod->pImgVt)
374 return VERR_DBG_NO_MATCHING_INTERPRETER;
375
376 /*
377 * Try load the module into an empty address space.
378 */
379 static uint32_t volatile s_uFakeHandle = 0x3940000;
380 HANDLE hFake;
381 do
382 hFake = (HANDLE)(uintptr_t)ASMAtomicIncU32(&s_uFakeHandle);
383 while (hFake == NULL || hFake == INVALID_HANDLE_VALUE);
384
385 int rc;
386 if (SymInitialize(hFake, NULL /*SearchPath*/, FALSE /*fInvalidProcess*/))
387 {
388 SymSetOptions(SYMOPT_LOAD_LINES | SymGetOptions());
389
390 PRTUTF16 pwszDbgFile;
391 rc = RTStrToUtf16(pMod->pszDbgFile, &pwszDbgFile);
392 if (RT_SUCCESS(rc))
393 {
394 uint64_t uModAddr = SymLoadModuleExW(hFake, NULL /*hFile*/, pwszDbgFile, NULL /*pszModName*/,
395 0 /*uLoadAddr*/, 0 /*cbImage*/, NULL /*pModData*/, 0 /*fFlags*/);
396 if (uModAddr != 0)
397 {
398 /*
399 * Create a container for copying the information into.
400 */
401 RTDBGMOD hCnt;
402 rc = RTDbgModCreate(&hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
403 if (RT_SUCCESS(rc))
404 {
405 rc = pMod->pImgVt->pfnEnumSegments(pMod, rtDbgModDbgHelpAddSegmentsCallback, hCnt);
406 if (RT_SUCCESS(rc))
407 rc = rtDbgModDbgHelpCopySymbols(pMod, hCnt, hFake, uModAddr);
408 if (RT_SUCCESS(rc))
409 rc = rtDbgModDbgHelpCopyLineNumbers(pMod, hCnt, hFake, uModAddr);
410 if (RT_SUCCESS(rc))
411 {
412 pMod->pvDbgPriv = hCnt;
413 pMod->pDbgVt = &g_rtDbgModVtDbgDbgHelp;
414 hCnt = NIL_RTDBGMOD;
415 }
416 RTDbgModRelease(hCnt);
417 }
418
419 SymUnloadModule64(hFake, uModAddr);
420 }
421 else
422 rc = RTErrConvertFromWin32(GetLastError());
423 RTUtf16Free(pwszDbgFile);
424 }
425
426 BOOL fRc2 = SymCleanup(hFake); Assert(fRc2); NOREF(fRc2);
427 }
428 else
429 rc = RTErrConvertFromWin32(GetLastError());
430 return rc;
431}
432
433
434
435/** Virtual function table for the DBGHELP debug info reader. */
436DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDbgHelp =
437{
438 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
439 /*.fSupports = */ RT_DBGTYPE_CODEVIEW,
440 /*.pszName = */ "dbghelp",
441 /*.pfnTryOpen = */ rtDbgModDbgHelp_TryOpen,
442 /*.pfnClose = */ rtDbgModDbgHelp_Close,
443
444 /*.pfnRvaToSegOff = */ rtDbgModDbgHelp_RvaToSegOff,
445 /*.pfnImageSize = */ rtDbgModDbgHelp_ImageSize,
446
447 /*.pfnSegmentAdd = */ rtDbgModDbgHelp_SegmentAdd,
448 /*.pfnSegmentCount = */ rtDbgModDbgHelp_SegmentCount,
449 /*.pfnSegmentByIndex = */ rtDbgModDbgHelp_SegmentByIndex,
450
451 /*.pfnSymbolAdd = */ rtDbgModDbgHelp_SymbolAdd,
452 /*.pfnSymbolCount = */ rtDbgModDbgHelp_SymbolCount,
453 /*.pfnSymbolByOrdinal = */ rtDbgModDbgHelp_SymbolByOrdinal,
454 /*.pfnSymbolByName = */ rtDbgModDbgHelp_SymbolByName,
455 /*.pfnSymbolByAddr = */ rtDbgModDbgHelp_SymbolByAddr,
456
457 /*.pfnLineAdd = */ rtDbgModDbgHelp_LineAdd,
458 /*.pfnLineCount = */ rtDbgModDbgHelp_LineCount,
459 /*.pfnLineByOrdinal = */ rtDbgModDbgHelp_LineByOrdinal,
460 /*.pfnLineByAddr = */ rtDbgModDbgHelp_LineByAddr,
461
462 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
463};
464
465
Note: See TracBrowser for help on using the repository browser.

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