1 | /* $Id: dbgmoddbghelp.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Debug Info Reader Using DbgHelp.dll if Present.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2017 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 <iprt/win/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]); RT_NOREF_PV(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 | RT_NOREF_PV(hLdrMod);
|
---|
367 |
|
---|
368 | Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
|
---|
369 | pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
|
---|
370 |
|
---|
371 | Assert(pSeg->cchName > 0);
|
---|
372 | Assert(!pSeg->pszName[pSeg->cchName]);
|
---|
373 |
|
---|
374 | if (!pSeg->RVA)
|
---|
375 | pArgs->uModAddr = pSeg->LinkAddress;
|
---|
376 |
|
---|
377 | RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
|
---|
378 | return RTDbgModSegmentAdd(pArgs->hCnt, pSeg->RVA, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
|
---|
379 | }
|
---|
380 |
|
---|
381 |
|
---|
382 | /** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
|
---|
383 | static DECLCALLBACK(int) rtDbgModDbgHelp_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
|
---|
384 | {
|
---|
385 | NOREF(enmArch);
|
---|
386 |
|
---|
387 | /*
|
---|
388 | * Currently only support external files with a executable already present.
|
---|
389 | */
|
---|
390 | if (!pMod->pszDbgFile)
|
---|
391 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
392 | if (!pMod->pImgVt)
|
---|
393 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Create a container for copying the information into. We do this early
|
---|
397 | * so we can determine the image base address.
|
---|
398 | */
|
---|
399 | RTDBGMOD hCnt;
|
---|
400 | int rc = RTDbgModCreate(&hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
|
---|
401 | if (RT_SUCCESS(rc))
|
---|
402 | {
|
---|
403 | RTDBGMODBGHELPARGS Args;
|
---|
404 | RT_ZERO(Args);
|
---|
405 | Args.hCnt = hCnt;
|
---|
406 | rc = pMod->pImgVt->pfnEnumSegments(pMod, rtDbgModDbgHelpAddSegmentsCallback, &Args);
|
---|
407 | if (RT_SUCCESS(rc))
|
---|
408 | {
|
---|
409 | uint32_t cbImage = pMod->pImgVt->pfnImageSize(pMod);
|
---|
410 | uint64_t uImageBase = Args.uModAddr ? Args.uModAddr : 0x4000000;
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * Try load the module into an empty address space.
|
---|
414 | */
|
---|
415 | static uint32_t volatile s_uFakeHandle = 0x3940000;
|
---|
416 | HANDLE hFake;
|
---|
417 | do
|
---|
418 | hFake = (HANDLE)(uintptr_t)ASMAtomicIncU32(&s_uFakeHandle);
|
---|
419 | while (hFake == NULL || hFake == INVALID_HANDLE_VALUE);
|
---|
420 |
|
---|
421 | LogFlow(("rtDbgModDbgHelp_TryOpen: \n"));
|
---|
422 | if (SymInitialize(hFake, NULL /*SearchPath*/, FALSE /*fInvalidProcess*/))
|
---|
423 | {
|
---|
424 | SymSetOptions(SYMOPT_LOAD_LINES | SymGetOptions());
|
---|
425 |
|
---|
426 | PRTUTF16 pwszDbgFile;
|
---|
427 | rc = RTStrToUtf16(pMod->pszDbgFile, &pwszDbgFile);
|
---|
428 | if (RT_SUCCESS(rc))
|
---|
429 | {
|
---|
430 | uint64_t uModAddr = SymLoadModuleExW(hFake, NULL /*hFile*/, pwszDbgFile, NULL /*pszModName*/,
|
---|
431 | uImageBase, cbImage, NULL /*pModData*/, 0 /*fFlags*/);
|
---|
432 | if (uModAddr != 0)
|
---|
433 | {
|
---|
434 | rc = rtDbgModDbgHelpCopySymbols(pMod, hCnt, hFake, uModAddr);
|
---|
435 | if (RT_SUCCESS(rc))
|
---|
436 | rc = rtDbgModDbgHelpCopyLineNumbers(pMod, hCnt, hFake, uModAddr);
|
---|
437 | if (RT_SUCCESS(rc))
|
---|
438 | {
|
---|
439 | pMod->pvDbgPriv = hCnt;
|
---|
440 | pMod->pDbgVt = &g_rtDbgModVtDbgDbgHelp;
|
---|
441 | hCnt = NIL_RTDBGMOD;
|
---|
442 | LogFlow(("rtDbgModDbgHelp_TryOpen: Successfully loaded '%s' at %#llx\n",
|
---|
443 | pMod->pszDbgFile, (uint64_t)uImageBase));
|
---|
444 | }
|
---|
445 |
|
---|
446 | SymUnloadModule64(hFake, uModAddr);
|
---|
447 | }
|
---|
448 | else
|
---|
449 | {
|
---|
450 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
451 | LogFlow(("rtDbgModDbgHelp_TryOpen: Error loading the module '%s' at %#llx: %Rrc (%u)\n",
|
---|
452 | pMod->pszDbgFile, (uint64_t)uImageBase, rc, GetLastError()));
|
---|
453 | }
|
---|
454 | RTUtf16Free(pwszDbgFile);
|
---|
455 | }
|
---|
456 | else
|
---|
457 | LogFlow(("rtDbgModDbgHelp_TryOpen: Unicode version issue: %Rrc\n", rc));
|
---|
458 |
|
---|
459 | BOOL fRc2 = SymCleanup(hFake); Assert(fRc2); NOREF(fRc2);
|
---|
460 | }
|
---|
461 | else
|
---|
462 | {
|
---|
463 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
464 | LogFlow(("rtDbgModDbgHelp_TryOpen: SymInitialize failed: %Rrc (%u)\n", rc, GetLastError()));
|
---|
465 | }
|
---|
466 | }
|
---|
467 | RTDbgModRelease(hCnt);
|
---|
468 | }
|
---|
469 | return rc;
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 |
|
---|
474 | /** Virtual function table for the DBGHELP debug info reader. */
|
---|
475 | DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDbgHelp =
|
---|
476 | {
|
---|
477 | /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
|
---|
478 | /*.fSupports = */ RT_DBGTYPE_CODEVIEW,
|
---|
479 | /*.pszName = */ "dbghelp",
|
---|
480 | /*.pfnTryOpen = */ rtDbgModDbgHelp_TryOpen,
|
---|
481 | /*.pfnClose = */ rtDbgModDbgHelp_Close,
|
---|
482 |
|
---|
483 | /*.pfnRvaToSegOff = */ rtDbgModDbgHelp_RvaToSegOff,
|
---|
484 | /*.pfnImageSize = */ rtDbgModDbgHelp_ImageSize,
|
---|
485 |
|
---|
486 | /*.pfnSegmentAdd = */ rtDbgModDbgHelp_SegmentAdd,
|
---|
487 | /*.pfnSegmentCount = */ rtDbgModDbgHelp_SegmentCount,
|
---|
488 | /*.pfnSegmentByIndex = */ rtDbgModDbgHelp_SegmentByIndex,
|
---|
489 |
|
---|
490 | /*.pfnSymbolAdd = */ rtDbgModDbgHelp_SymbolAdd,
|
---|
491 | /*.pfnSymbolCount = */ rtDbgModDbgHelp_SymbolCount,
|
---|
492 | /*.pfnSymbolByOrdinal = */ rtDbgModDbgHelp_SymbolByOrdinal,
|
---|
493 | /*.pfnSymbolByName = */ rtDbgModDbgHelp_SymbolByName,
|
---|
494 | /*.pfnSymbolByAddr = */ rtDbgModDbgHelp_SymbolByAddr,
|
---|
495 |
|
---|
496 | /*.pfnLineAdd = */ rtDbgModDbgHelp_LineAdd,
|
---|
497 | /*.pfnLineCount = */ rtDbgModDbgHelp_LineCount,
|
---|
498 | /*.pfnLineByOrdinal = */ rtDbgModDbgHelp_LineByOrdinal,
|
---|
499 | /*.pfnLineByAddr = */ rtDbgModDbgHelp_LineByAddr,
|
---|
500 |
|
---|
501 | /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
|
---|
502 | };
|
---|
503 |
|
---|