1 | /* $Id: dbgmoddbghelp.cpp 62477 2016-07-22 18:27:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Debug Info Reader Using DbgHelp.dll if Present.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2016 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. */
|
---|
54 | typedef 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} */
|
---|
71 | static 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} */
|
---|
80 | static 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} */
|
---|
88 | static 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} */
|
---|
96 | static 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} */
|
---|
106 | static 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} */
|
---|
115 | static 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} */
|
---|
125 | static 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} */
|
---|
133 | static 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} */
|
---|
141 | static 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} */
|
---|
152 | static 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} */
|
---|
160 | static 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} */
|
---|
168 | static 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} */
|
---|
178 | static 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} */
|
---|
188 | static 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} */
|
---|
196 | static 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 | */
|
---|
212 | static BOOL CALLBACK rtDbgModDbgHelpCopyLineNumberCallback(PSRCCODEINFOW pLineInfo, PVOID pvUser)
|
---|
213 | {
|
---|
214 | RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
|
---|
215 |
|
---|
216 | if (pLineInfo->Address < pArgs->uModAddr)
|
---|
217 | {
|
---|
218 | Log((" %#018RX64 %05u %s [SKIPPED - INVALID ADDRESS!]\n", pLineInfo->Address, pLineInfo->LineNumber));
|
---|
219 | return TRUE;
|
---|
220 | }
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * To save having to call RTUtf16ToUtf8 every time, we keep a copy of the
|
---|
224 | * previous file name both as UTF-8 and UTF-16.
|
---|
225 | */
|
---|
226 | /** @todo we could combine RTUtf16Len and memcmp... */
|
---|
227 | size_t cbLen = (RTUtf16Len(pLineInfo->FileName) + 1) * sizeof(RTUTF16);
|
---|
228 | if ( !pArgs->pwszPrev
|
---|
229 | || memcmp(pArgs->pwszPrev, pLineInfo->FileName, cbLen) )
|
---|
230 | {
|
---|
231 | if (pArgs->cbPrevUtf16Alloc >= cbLen)
|
---|
232 | memcpy(pArgs->pwszPrev, pLineInfo->FileName, cbLen);
|
---|
233 | else
|
---|
234 | {
|
---|
235 | RTMemFree(pArgs->pwszPrev);
|
---|
236 | pArgs->cbPrevUtf16Alloc = cbLen;
|
---|
237 | pArgs->pwszPrev = (PRTUTF16)RTMemDupEx(pLineInfo->FileName, cbLen, pArgs->cbPrevUtf16Alloc - cbLen);
|
---|
238 | if (!pArgs->pwszPrev)
|
---|
239 | pArgs->cbPrevUtf16Alloc = 0;
|
---|
240 | }
|
---|
241 |
|
---|
242 | RTStrFree(pArgs->pszPrev);
|
---|
243 | pArgs->pszPrev = NULL;
|
---|
244 | int rc = RTUtf16ToUtf8(pLineInfo->FileName, &pArgs->pszPrev);
|
---|
245 | if (RT_FAILURE(rc))
|
---|
246 | {
|
---|
247 | SetLastError(ERROR_OUTOFMEMORY);
|
---|
248 | Log(("rtDbgModDbgHelpCopyLineNumberCallback: Out of memory\n"));
|
---|
249 | return FALSE;
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * Add the line number to the container.
|
---|
255 | */
|
---|
256 | int rc = RTDbgModLineAdd(pArgs->hCnt, pArgs->pszPrev, pLineInfo->LineNumber,
|
---|
257 | RTDBGSEGIDX_RVA, pLineInfo->Address - pArgs->uModAddr, NULL);
|
---|
258 | Log((" %#018RX64 %05u %s [%Rrc]\n", pLineInfo->Address, pLineInfo->LineNumber, pArgs->pszPrev, rc));
|
---|
259 | NOREF(rc);
|
---|
260 |
|
---|
261 | return TRUE;
|
---|
262 | }
|
---|
263 |
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Copies the line numbers into the container.
|
---|
267 | *
|
---|
268 | * @returns IPRT status code.
|
---|
269 | * @param pMod The debug module.
|
---|
270 | * @param hCnt The container that will keep the symbols.
|
---|
271 | * @param hFake The fake process handle.
|
---|
272 | * @param uModAddr The module load address.
|
---|
273 | */
|
---|
274 | static int rtDbgModDbgHelpCopyLineNumbers(PRTDBGMODINT pMod, RTDBGMOD hCnt, HANDLE hFake, uint64_t uModAddr)
|
---|
275 | {
|
---|
276 | RTDBGMODBGHELPARGS Args;
|
---|
277 | Args.hCnt = hCnt;
|
---|
278 | Args.pMod = pMod;
|
---|
279 | Args.uModAddr = uModAddr;
|
---|
280 | Args.pszPrev = NULL;
|
---|
281 | Args.pwszPrev = NULL;
|
---|
282 | Args.cbPrevUtf16Alloc = 0;
|
---|
283 |
|
---|
284 | int rc;
|
---|
285 | if (SymEnumLinesW(hFake, uModAddr, NULL /*pszObj*/, NULL /*pszFile*/, rtDbgModDbgHelpCopyLineNumberCallback, &Args))
|
---|
286 | rc = VINF_SUCCESS;
|
---|
287 | else
|
---|
288 | {
|
---|
289 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
290 | Log(("Line number enum: %Rrc (%u)\n", rc, GetLastError()));
|
---|
291 | if (rc == VERR_NOT_SUPPORTED)
|
---|
292 | rc = VINF_SUCCESS;
|
---|
293 | }
|
---|
294 |
|
---|
295 | RTStrFree(Args.pszPrev);
|
---|
296 | RTMemFree(Args.pwszPrev);
|
---|
297 | return rc;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * SymEnumSymbols callback that adds a symbol to the container.
|
---|
303 | *
|
---|
304 | * @returns TRUE
|
---|
305 | * @param pSymInfo The symbol information.
|
---|
306 | * @param cbSymbol The symbol size (estimated).
|
---|
307 | * @param pvUser Pointer to a RTDBGMODBGHELPARGS structure.
|
---|
308 | */
|
---|
309 | static BOOL CALLBACK rtDbgModDbgHelpCopySymbolsCallback(PSYMBOL_INFO pSymInfo, ULONG cbSymbol, PVOID pvUser)
|
---|
310 | {
|
---|
311 | RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
|
---|
312 | if (pSymInfo->Address < pArgs->uModAddr) /* NT4 SP1 ntfs.dbg */
|
---|
313 | {
|
---|
314 | Log((" %#018RX64 LB %#07x %s [SKIPPED - INVALID ADDRESS!]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name));
|
---|
315 | return TRUE;
|
---|
316 | }
|
---|
317 | if (pSymInfo->NameLen >= RTDBG_SYMBOL_NAME_LENGTH)
|
---|
318 | {
|
---|
319 | Log((" %#018RX64 LB %#07x %s [SKIPPED - TOO LONG (%u > %u)!]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name,
|
---|
320 | pSymInfo->NameLen, RTDBG_SYMBOL_NAME_LENGTH));
|
---|
321 | return TRUE;
|
---|
322 | }
|
---|
323 |
|
---|
324 | /* ASSUMES the symbol name is ASCII. */
|
---|
325 | int rc = RTDbgModSymbolAdd(pArgs->hCnt, pSymInfo->Name, RTDBGSEGIDX_RVA,
|
---|
326 | pSymInfo->Address - pArgs->uModAddr, cbSymbol, 0, NULL);
|
---|
327 | Log((" %#018RX64 LB %#07x %s [%Rrc]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name, rc));
|
---|
328 | NOREF(rc);
|
---|
329 |
|
---|
330 | return TRUE;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Copies the symbols into the container.
|
---|
336 | *
|
---|
337 | * @returns IPRT status code.
|
---|
338 | * @param pMod The debug module.
|
---|
339 | * @param hCnt The container that will keep the symbols.
|
---|
340 | * @param hFake The fake process handle.
|
---|
341 | * @param uModAddr The module load address.
|
---|
342 | */
|
---|
343 | static int rtDbgModDbgHelpCopySymbols(PRTDBGMODINT pMod, RTDBGMOD hCnt, HANDLE hFake, uint64_t uModAddr)
|
---|
344 | {
|
---|
345 | RTDBGMODBGHELPARGS Args;
|
---|
346 | Args.hCnt = hCnt;
|
---|
347 | Args.pMod = pMod;
|
---|
348 | Args.uModAddr = uModAddr;
|
---|
349 | int rc;
|
---|
350 | if (SymEnumSymbols(hFake, uModAddr, NULL, rtDbgModDbgHelpCopySymbolsCallback, &Args))
|
---|
351 | rc = VINF_SUCCESS;
|
---|
352 | else
|
---|
353 | {
|
---|
354 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
355 | Log(("SymEnumSymbols: %Rrc (%u)\n", rc, GetLastError()));
|
---|
356 | }
|
---|
357 | return rc;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | /** @callback_method_impl{FNRTLDRENUMSEGS, Copies the PE segments over into
|
---|
362 | * the container.} */
|
---|
363 | static DECLCALLBACK(int) rtDbgModDbgHelpAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
|
---|
364 | {
|
---|
365 | RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
|
---|
366 |
|
---|
367 | Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
|
---|
368 | pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
|
---|
369 |
|
---|
370 | Assert(pSeg->cchName > 0);
|
---|
371 | Assert(!pSeg->pszName[pSeg->cchName]);
|
---|
372 |
|
---|
373 | if (!pSeg->RVA)
|
---|
374 | pArgs->uModAddr = pSeg->LinkAddress;
|
---|
375 |
|
---|
376 | RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
|
---|
377 | return RTDbgModSegmentAdd(pArgs->hCnt, pSeg->RVA, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
|
---|
378 | }
|
---|
379 |
|
---|
380 |
|
---|
381 | /** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
|
---|
382 | static DECLCALLBACK(int) rtDbgModDbgHelp_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
|
---|
383 | {
|
---|
384 | NOREF(enmArch);
|
---|
385 |
|
---|
386 | /*
|
---|
387 | * Currently only support external files with a executable already present.
|
---|
388 | */
|
---|
389 | if (!pMod->pszDbgFile)
|
---|
390 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
391 | if (!pMod->pImgVt)
|
---|
392 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Create a container for copying the information into. We do this early
|
---|
396 | * so we can determine the image base address.
|
---|
397 | */
|
---|
398 | RTDBGMOD hCnt;
|
---|
399 | int rc = RTDbgModCreate(&hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
|
---|
400 | if (RT_SUCCESS(rc))
|
---|
401 | {
|
---|
402 | RTDBGMODBGHELPARGS Args;
|
---|
403 | RT_ZERO(Args);
|
---|
404 | Args.hCnt = hCnt;
|
---|
405 | rc = pMod->pImgVt->pfnEnumSegments(pMod, rtDbgModDbgHelpAddSegmentsCallback, &Args);
|
---|
406 | if (RT_SUCCESS(rc))
|
---|
407 | {
|
---|
408 | uint32_t cbImage = pMod->pImgVt->pfnImageSize(pMod);
|
---|
409 | uint64_t uImageBase = Args.uModAddr ? Args.uModAddr : 0x4000000;
|
---|
410 |
|
---|
411 | /*
|
---|
412 | * Try load the module into an empty address space.
|
---|
413 | */
|
---|
414 | static uint32_t volatile s_uFakeHandle = 0x3940000;
|
---|
415 | HANDLE hFake;
|
---|
416 | do
|
---|
417 | hFake = (HANDLE)(uintptr_t)ASMAtomicIncU32(&s_uFakeHandle);
|
---|
418 | while (hFake == NULL || hFake == INVALID_HANDLE_VALUE);
|
---|
419 |
|
---|
420 | LogFlow(("rtDbgModDbgHelp_TryOpen: \n"));
|
---|
421 | if (SymInitialize(hFake, NULL /*SearchPath*/, FALSE /*fInvalidProcess*/))
|
---|
422 | {
|
---|
423 | SymSetOptions(SYMOPT_LOAD_LINES | SymGetOptions());
|
---|
424 |
|
---|
425 | PRTUTF16 pwszDbgFile;
|
---|
426 | rc = RTStrToUtf16(pMod->pszDbgFile, &pwszDbgFile);
|
---|
427 | if (RT_SUCCESS(rc))
|
---|
428 | {
|
---|
429 | uint64_t uModAddr = SymLoadModuleExW(hFake, NULL /*hFile*/, pwszDbgFile, NULL /*pszModName*/,
|
---|
430 | uImageBase, cbImage, NULL /*pModData*/, 0 /*fFlags*/);
|
---|
431 | if (uModAddr != 0)
|
---|
432 | {
|
---|
433 | rc = rtDbgModDbgHelpCopySymbols(pMod, hCnt, hFake, uModAddr);
|
---|
434 | if (RT_SUCCESS(rc))
|
---|
435 | rc = rtDbgModDbgHelpCopyLineNumbers(pMod, hCnt, hFake, uModAddr);
|
---|
436 | if (RT_SUCCESS(rc))
|
---|
437 | {
|
---|
438 | pMod->pvDbgPriv = hCnt;
|
---|
439 | pMod->pDbgVt = &g_rtDbgModVtDbgDbgHelp;
|
---|
440 | hCnt = NIL_RTDBGMOD;
|
---|
441 | LogFlow(("rtDbgModDbgHelp_TryOpen: Successfully loaded '%s' at %#llx\n",
|
---|
442 | pMod->pszDbgFile, (uint64_t)uImageBase));
|
---|
443 | }
|
---|
444 |
|
---|
445 | SymUnloadModule64(hFake, uModAddr);
|
---|
446 | }
|
---|
447 | else
|
---|
448 | {
|
---|
449 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
450 | LogFlow(("rtDbgModDbgHelp_TryOpen: Error loading the module '%s' at %#llx: %Rrc (%u)\n",
|
---|
451 | pMod->pszDbgFile, (uint64_t)uImageBase, rc, GetLastError()));
|
---|
452 | }
|
---|
453 | RTUtf16Free(pwszDbgFile);
|
---|
454 | }
|
---|
455 | else
|
---|
456 | LogFlow(("rtDbgModDbgHelp_TryOpen: Unicode version issue: %Rrc\n", rc));
|
---|
457 |
|
---|
458 | BOOL fRc2 = SymCleanup(hFake); Assert(fRc2); NOREF(fRc2);
|
---|
459 | }
|
---|
460 | else
|
---|
461 | {
|
---|
462 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
463 | LogFlow(("rtDbgModDbgHelp_TryOpen: SymInitialize failed: %Rrc (%u)\n", rc, GetLastError()));
|
---|
464 | }
|
---|
465 | }
|
---|
466 | RTDbgModRelease(hCnt);
|
---|
467 | }
|
---|
468 | return rc;
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 |
|
---|
473 | /** Virtual function table for the DBGHELP debug info reader. */
|
---|
474 | DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDbgHelp =
|
---|
475 | {
|
---|
476 | /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
|
---|
477 | /*.fSupports = */ RT_DBGTYPE_CODEVIEW,
|
---|
478 | /*.pszName = */ "dbghelp",
|
---|
479 | /*.pfnTryOpen = */ rtDbgModDbgHelp_TryOpen,
|
---|
480 | /*.pfnClose = */ rtDbgModDbgHelp_Close,
|
---|
481 |
|
---|
482 | /*.pfnRvaToSegOff = */ rtDbgModDbgHelp_RvaToSegOff,
|
---|
483 | /*.pfnImageSize = */ rtDbgModDbgHelp_ImageSize,
|
---|
484 |
|
---|
485 | /*.pfnSegmentAdd = */ rtDbgModDbgHelp_SegmentAdd,
|
---|
486 | /*.pfnSegmentCount = */ rtDbgModDbgHelp_SegmentCount,
|
---|
487 | /*.pfnSegmentByIndex = */ rtDbgModDbgHelp_SegmentByIndex,
|
---|
488 |
|
---|
489 | /*.pfnSymbolAdd = */ rtDbgModDbgHelp_SymbolAdd,
|
---|
490 | /*.pfnSymbolCount = */ rtDbgModDbgHelp_SymbolCount,
|
---|
491 | /*.pfnSymbolByOrdinal = */ rtDbgModDbgHelp_SymbolByOrdinal,
|
---|
492 | /*.pfnSymbolByName = */ rtDbgModDbgHelp_SymbolByName,
|
---|
493 | /*.pfnSymbolByAddr = */ rtDbgModDbgHelp_SymbolByAddr,
|
---|
494 |
|
---|
495 | /*.pfnLineAdd = */ rtDbgModDbgHelp_LineAdd,
|
---|
496 | /*.pfnLineCount = */ rtDbgModDbgHelp_LineCount,
|
---|
497 | /*.pfnLineByOrdinal = */ rtDbgModDbgHelp_LineByOrdinal,
|
---|
498 | /*.pfnLineByAddr = */ rtDbgModDbgHelp_LineByAddr,
|
---|
499 |
|
---|
500 | /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
|
---|
501 | };
|
---|
502 |
|
---|