1 | /* $Id: dbgmoddbghelp.cpp 73494 2018-08-04 19:41:30Z 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 | /** @interface_method_impl{RTDBGMODVTDBG,pfnUnwindFrame} */
|
---|
70 | static DECLCALLBACK(int) rtDbgModDbgHelp_UnwindFrame(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
|
---|
71 | {
|
---|
72 | RT_NOREF(pMod, iSeg, off, pState);
|
---|
73 | return VERR_DBG_NO_UNWIND_INFO;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 |
|
---|
78 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
|
---|
79 | static DECLCALLBACK(int) rtDbgModDbgHelp_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
|
---|
80 | PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
|
---|
81 | {
|
---|
82 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
83 | return RTDbgModLineByAddr(hCnt, iSeg, off, poffDisp, pLineInfo);
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
|
---|
88 | static DECLCALLBACK(int) rtDbgModDbgHelp_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
|
---|
89 | {
|
---|
90 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
91 | return RTDbgModLineByOrdinal(hCnt, iOrdinal, pLineInfo);
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
|
---|
96 | static DECLCALLBACK(uint32_t) rtDbgModDbgHelp_LineCount(PRTDBGMODINT pMod)
|
---|
97 | {
|
---|
98 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
99 | return RTDbgModLineCount(hCnt);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | /** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
|
---|
104 | static DECLCALLBACK(int) rtDbgModDbgHelp_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
|
---|
105 | uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
|
---|
106 | {
|
---|
107 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
108 | Assert(!pszFile[cchFile]); NOREF(cchFile);
|
---|
109 | return RTDbgModLineAdd(hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
|
---|
114 | static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
|
---|
115 | PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
|
---|
116 | {
|
---|
117 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
118 | return RTDbgModSymbolByAddr(hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
|
---|
123 | static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
|
---|
124 | PRTDBGSYMBOL pSymInfo)
|
---|
125 | {
|
---|
126 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
127 | Assert(!pszSymbol[cchSymbol]); RT_NOREF_PV(cchSymbol);
|
---|
128 | return RTDbgModSymbolByName(hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
|
---|
133 | static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
|
---|
134 | {
|
---|
135 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
136 | return RTDbgModSymbolByOrdinal(hCnt, iOrdinal, pSymInfo);
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
|
---|
141 | static DECLCALLBACK(uint32_t) rtDbgModDbgHelp_SymbolCount(PRTDBGMODINT pMod)
|
---|
142 | {
|
---|
143 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
144 | return RTDbgModSymbolCount(hCnt);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
|
---|
149 | static DECLCALLBACK(int) rtDbgModDbgHelp_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
|
---|
150 | RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
|
---|
151 | uint32_t *piOrdinal)
|
---|
152 | {
|
---|
153 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
154 | Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
|
---|
155 | return RTDbgModSymbolAdd(hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
|
---|
160 | static DECLCALLBACK(int) rtDbgModDbgHelp_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
|
---|
161 | {
|
---|
162 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
163 | return RTDbgModSegmentByIndex(hCnt, iSeg, pSegInfo);
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
|
---|
168 | static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDbgHelp_SegmentCount(PRTDBGMODINT pMod)
|
---|
169 | {
|
---|
170 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
171 | return RTDbgModSegmentCount(hCnt);
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
|
---|
176 | static DECLCALLBACK(int) rtDbgModDbgHelp_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
|
---|
177 | uint32_t fFlags, PRTDBGSEGIDX piSeg)
|
---|
178 | {
|
---|
179 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
180 | Assert(!pszName[cchName]); NOREF(cchName);
|
---|
181 | return RTDbgModSegmentAdd(hCnt, uRva, cb, pszName, fFlags, piSeg);
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
|
---|
186 | static DECLCALLBACK(RTUINTPTR) rtDbgModDbgHelp_ImageSize(PRTDBGMODINT pMod)
|
---|
187 | {
|
---|
188 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
189 | RTUINTPTR cb1 = RTDbgModImageSize(hCnt);
|
---|
190 | RTUINTPTR cb2 = pMod->pImgVt->pfnImageSize(pMod);
|
---|
191 | return RT_MAX(cb1, cb2);
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | /** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
|
---|
196 | static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDbgHelp_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
|
---|
197 | {
|
---|
198 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;
|
---|
199 | return RTDbgModRvaToSegOff(hCnt, uRva, poffSeg);
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | /** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
|
---|
204 | static DECLCALLBACK(int) rtDbgModDbgHelp_Close(PRTDBGMODINT pMod)
|
---|
205 | {
|
---|
206 | RTDBGMOD hCnt = (RTDBGMOD)pMod->pvDbgPriv;;
|
---|
207 | RTDbgModRelease(hCnt);
|
---|
208 | pMod->pvDbgPriv = NULL;
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * SymEnumLinesW callback that adds a line number to the container.
|
---|
215 | *
|
---|
216 | * @returns TRUE, FALSE if we're out of memory.
|
---|
217 | * @param pLineInfo Line number information.
|
---|
218 | * @param pvUser Pointer to a RTDBGMODBGHELPARGS structure.
|
---|
219 | */
|
---|
220 | static BOOL CALLBACK rtDbgModDbgHelpCopyLineNumberCallback(PSRCCODEINFOW pLineInfo, PVOID pvUser)
|
---|
221 | {
|
---|
222 | RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
|
---|
223 |
|
---|
224 | if (pLineInfo->Address < pArgs->uModAddr)
|
---|
225 | {
|
---|
226 | Log((" %#018RX64 %05u %s [SKIPPED - INVALID ADDRESS!]\n", pLineInfo->Address, pLineInfo->LineNumber));
|
---|
227 | return TRUE;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * To save having to call RTUtf16ToUtf8 every time, we keep a copy of the
|
---|
232 | * previous file name both as UTF-8 and UTF-16.
|
---|
233 | */
|
---|
234 | /** @todo we could combine RTUtf16Len and memcmp... */
|
---|
235 | size_t cbLen = (RTUtf16Len(pLineInfo->FileName) + 1) * sizeof(RTUTF16);
|
---|
236 | if ( !pArgs->pwszPrev
|
---|
237 | || memcmp(pArgs->pwszPrev, pLineInfo->FileName, cbLen) )
|
---|
238 | {
|
---|
239 | if (pArgs->cbPrevUtf16Alloc >= cbLen)
|
---|
240 | memcpy(pArgs->pwszPrev, pLineInfo->FileName, cbLen);
|
---|
241 | else
|
---|
242 | {
|
---|
243 | RTMemFree(pArgs->pwszPrev);
|
---|
244 | pArgs->cbPrevUtf16Alloc = cbLen;
|
---|
245 | pArgs->pwszPrev = (PRTUTF16)RTMemDupEx(pLineInfo->FileName, cbLen, pArgs->cbPrevUtf16Alloc - cbLen);
|
---|
246 | if (!pArgs->pwszPrev)
|
---|
247 | pArgs->cbPrevUtf16Alloc = 0;
|
---|
248 | }
|
---|
249 |
|
---|
250 | RTStrFree(pArgs->pszPrev);
|
---|
251 | pArgs->pszPrev = NULL;
|
---|
252 | int rc = RTUtf16ToUtf8(pLineInfo->FileName, &pArgs->pszPrev);
|
---|
253 | if (RT_FAILURE(rc))
|
---|
254 | {
|
---|
255 | SetLastError(ERROR_OUTOFMEMORY);
|
---|
256 | Log(("rtDbgModDbgHelpCopyLineNumberCallback: Out of memory\n"));
|
---|
257 | return FALSE;
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * Add the line number to the container.
|
---|
263 | */
|
---|
264 | int rc = RTDbgModLineAdd(pArgs->hCnt, pArgs->pszPrev, pLineInfo->LineNumber,
|
---|
265 | RTDBGSEGIDX_RVA, pLineInfo->Address - pArgs->uModAddr, NULL);
|
---|
266 | Log((" %#018RX64 %05u %s [%Rrc]\n", pLineInfo->Address, pLineInfo->LineNumber, pArgs->pszPrev, rc));
|
---|
267 | NOREF(rc);
|
---|
268 |
|
---|
269 | return TRUE;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Copies the line numbers into the container.
|
---|
275 | *
|
---|
276 | * @returns IPRT status code.
|
---|
277 | * @param pMod The debug module.
|
---|
278 | * @param hCnt The container that will keep the symbols.
|
---|
279 | * @param hFake The fake process handle.
|
---|
280 | * @param uModAddr The module load address.
|
---|
281 | */
|
---|
282 | static int rtDbgModDbgHelpCopyLineNumbers(PRTDBGMODINT pMod, RTDBGMOD hCnt, HANDLE hFake, uint64_t uModAddr)
|
---|
283 | {
|
---|
284 | RTDBGMODBGHELPARGS Args;
|
---|
285 | Args.hCnt = hCnt;
|
---|
286 | Args.pMod = pMod;
|
---|
287 | Args.uModAddr = uModAddr;
|
---|
288 | Args.pszPrev = NULL;
|
---|
289 | Args.pwszPrev = NULL;
|
---|
290 | Args.cbPrevUtf16Alloc = 0;
|
---|
291 |
|
---|
292 | int rc;
|
---|
293 | if (SymEnumLinesW(hFake, uModAddr, NULL /*pszObj*/, NULL /*pszFile*/, rtDbgModDbgHelpCopyLineNumberCallback, &Args))
|
---|
294 | rc = VINF_SUCCESS;
|
---|
295 | else
|
---|
296 | {
|
---|
297 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
298 | Log(("Line number enum: %Rrc (%u)\n", rc, GetLastError()));
|
---|
299 | if (rc == VERR_NOT_SUPPORTED)
|
---|
300 | rc = VINF_SUCCESS;
|
---|
301 | }
|
---|
302 |
|
---|
303 | RTStrFree(Args.pszPrev);
|
---|
304 | RTMemFree(Args.pwszPrev);
|
---|
305 | return rc;
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | /**
|
---|
310 | * SymEnumSymbols callback that adds a symbol to the container.
|
---|
311 | *
|
---|
312 | * @returns TRUE
|
---|
313 | * @param pSymInfo The symbol information.
|
---|
314 | * @param cbSymbol The symbol size (estimated).
|
---|
315 | * @param pvUser Pointer to a RTDBGMODBGHELPARGS structure.
|
---|
316 | */
|
---|
317 | static BOOL CALLBACK rtDbgModDbgHelpCopySymbolsCallback(PSYMBOL_INFO pSymInfo, ULONG cbSymbol, PVOID pvUser)
|
---|
318 | {
|
---|
319 | RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
|
---|
320 | if (pSymInfo->Address < pArgs->uModAddr) /* NT4 SP1 ntfs.dbg */
|
---|
321 | {
|
---|
322 | Log((" %#018RX64 LB %#07x %s [SKIPPED - INVALID ADDRESS!]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name));
|
---|
323 | return TRUE;
|
---|
324 | }
|
---|
325 | if (pSymInfo->NameLen >= RTDBG_SYMBOL_NAME_LENGTH)
|
---|
326 | {
|
---|
327 | Log((" %#018RX64 LB %#07x %s [SKIPPED - TOO LONG (%u > %u)!]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name,
|
---|
328 | pSymInfo->NameLen, RTDBG_SYMBOL_NAME_LENGTH));
|
---|
329 | return TRUE;
|
---|
330 | }
|
---|
331 |
|
---|
332 | /* ASSUMES the symbol name is ASCII. */
|
---|
333 | int rc = RTDbgModSymbolAdd(pArgs->hCnt, pSymInfo->Name, RTDBGSEGIDX_RVA,
|
---|
334 | pSymInfo->Address - pArgs->uModAddr, cbSymbol, 0, NULL);
|
---|
335 | Log((" %#018RX64 LB %#07x %s [%Rrc]\n", pSymInfo->Address, cbSymbol, pSymInfo->Name, rc));
|
---|
336 | NOREF(rc);
|
---|
337 |
|
---|
338 | return TRUE;
|
---|
339 | }
|
---|
340 |
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Copies the symbols into the container.
|
---|
344 | *
|
---|
345 | * @returns IPRT status code.
|
---|
346 | * @param pMod The debug module.
|
---|
347 | * @param hCnt The container that will keep the symbols.
|
---|
348 | * @param hFake The fake process handle.
|
---|
349 | * @param uModAddr The module load address.
|
---|
350 | */
|
---|
351 | static int rtDbgModDbgHelpCopySymbols(PRTDBGMODINT pMod, RTDBGMOD hCnt, HANDLE hFake, uint64_t uModAddr)
|
---|
352 | {
|
---|
353 | RTDBGMODBGHELPARGS Args;
|
---|
354 | Args.hCnt = hCnt;
|
---|
355 | Args.pMod = pMod;
|
---|
356 | Args.uModAddr = uModAddr;
|
---|
357 | int rc;
|
---|
358 | if (SymEnumSymbols(hFake, uModAddr, NULL, rtDbgModDbgHelpCopySymbolsCallback, &Args))
|
---|
359 | rc = VINF_SUCCESS;
|
---|
360 | else
|
---|
361 | {
|
---|
362 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
363 | Log(("SymEnumSymbols: %Rrc (%u)\n", rc, GetLastError()));
|
---|
364 | }
|
---|
365 | return rc;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | /** @callback_method_impl{FNRTLDRENUMSEGS, Copies the PE segments over into
|
---|
370 | * the container.} */
|
---|
371 | static DECLCALLBACK(int) rtDbgModDbgHelpAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
|
---|
372 | {
|
---|
373 | RTDBGMODBGHELPARGS *pArgs = (RTDBGMODBGHELPARGS *)pvUser;
|
---|
374 | RT_NOREF_PV(hLdrMod);
|
---|
375 |
|
---|
376 | Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
|
---|
377 | pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
|
---|
378 |
|
---|
379 | Assert(pSeg->cchName > 0);
|
---|
380 | Assert(!pSeg->pszName[pSeg->cchName]);
|
---|
381 |
|
---|
382 | if (!pSeg->RVA)
|
---|
383 | pArgs->uModAddr = pSeg->LinkAddress;
|
---|
384 |
|
---|
385 | RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
|
---|
386 | return RTDbgModSegmentAdd(pArgs->hCnt, pSeg->RVA, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | /** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
|
---|
391 | static DECLCALLBACK(int) rtDbgModDbgHelp_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
|
---|
392 | {
|
---|
393 | NOREF(enmArch);
|
---|
394 |
|
---|
395 | /*
|
---|
396 | * Currently only support external files with a executable already present.
|
---|
397 | */
|
---|
398 | if (!pMod->pszDbgFile)
|
---|
399 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
400 | if (!pMod->pImgVt)
|
---|
401 | return VERR_DBG_NO_MATCHING_INTERPRETER;
|
---|
402 |
|
---|
403 | /*
|
---|
404 | * Create a container for copying the information into. We do this early
|
---|
405 | * so we can determine the image base address.
|
---|
406 | */
|
---|
407 | RTDBGMOD hCnt;
|
---|
408 | int rc = RTDbgModCreate(&hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
|
---|
409 | if (RT_SUCCESS(rc))
|
---|
410 | {
|
---|
411 | RTDBGMODBGHELPARGS Args;
|
---|
412 | RT_ZERO(Args);
|
---|
413 | Args.hCnt = hCnt;
|
---|
414 | rc = pMod->pImgVt->pfnEnumSegments(pMod, rtDbgModDbgHelpAddSegmentsCallback, &Args);
|
---|
415 | if (RT_SUCCESS(rc))
|
---|
416 | {
|
---|
417 | uint32_t cbImage = pMod->pImgVt->pfnImageSize(pMod);
|
---|
418 | uint64_t uImageBase = Args.uModAddr ? Args.uModAddr : 0x4000000;
|
---|
419 |
|
---|
420 | /*
|
---|
421 | * Try load the module into an empty address space.
|
---|
422 | */
|
---|
423 | static uint32_t volatile s_uFakeHandle = 0x3940000;
|
---|
424 | HANDLE hFake;
|
---|
425 | do
|
---|
426 | hFake = (HANDLE)(uintptr_t)ASMAtomicIncU32(&s_uFakeHandle);
|
---|
427 | while (hFake == NULL || hFake == INVALID_HANDLE_VALUE);
|
---|
428 |
|
---|
429 | LogFlow(("rtDbgModDbgHelp_TryOpen: \n"));
|
---|
430 | if (SymInitialize(hFake, NULL /*SearchPath*/, FALSE /*fInvalidProcess*/))
|
---|
431 | {
|
---|
432 | SymSetOptions(SYMOPT_LOAD_LINES | SymGetOptions());
|
---|
433 |
|
---|
434 | PRTUTF16 pwszDbgFile;
|
---|
435 | rc = RTStrToUtf16(pMod->pszDbgFile, &pwszDbgFile);
|
---|
436 | if (RT_SUCCESS(rc))
|
---|
437 | {
|
---|
438 | uint64_t uModAddr = SymLoadModuleExW(hFake, NULL /*hFile*/, pwszDbgFile, NULL /*pszModName*/,
|
---|
439 | uImageBase, cbImage, NULL /*pModData*/, 0 /*fFlags*/);
|
---|
440 | if (uModAddr != 0)
|
---|
441 | {
|
---|
442 | rc = rtDbgModDbgHelpCopySymbols(pMod, hCnt, hFake, uModAddr);
|
---|
443 | if (RT_SUCCESS(rc))
|
---|
444 | rc = rtDbgModDbgHelpCopyLineNumbers(pMod, hCnt, hFake, uModAddr);
|
---|
445 | if (RT_SUCCESS(rc))
|
---|
446 | {
|
---|
447 | pMod->pvDbgPriv = hCnt;
|
---|
448 | pMod->pDbgVt = &g_rtDbgModVtDbgDbgHelp;
|
---|
449 | hCnt = NIL_RTDBGMOD;
|
---|
450 | LogFlow(("rtDbgModDbgHelp_TryOpen: Successfully loaded '%s' at %#llx\n",
|
---|
451 | pMod->pszDbgFile, (uint64_t)uImageBase));
|
---|
452 | }
|
---|
453 |
|
---|
454 | SymUnloadModule64(hFake, uModAddr);
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {
|
---|
458 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
459 | LogFlow(("rtDbgModDbgHelp_TryOpen: Error loading the module '%s' at %#llx: %Rrc (%u)\n",
|
---|
460 | pMod->pszDbgFile, (uint64_t)uImageBase, rc, GetLastError()));
|
---|
461 | }
|
---|
462 | RTUtf16Free(pwszDbgFile);
|
---|
463 | }
|
---|
464 | else
|
---|
465 | LogFlow(("rtDbgModDbgHelp_TryOpen: Unicode version issue: %Rrc\n", rc));
|
---|
466 |
|
---|
467 | BOOL fRc2 = SymCleanup(hFake); Assert(fRc2); NOREF(fRc2);
|
---|
468 | }
|
---|
469 | else
|
---|
470 | {
|
---|
471 | rc = RTErrConvertFromWin32(GetLastError());
|
---|
472 | LogFlow(("rtDbgModDbgHelp_TryOpen: SymInitialize failed: %Rrc (%u)\n", rc, GetLastError()));
|
---|
473 | }
|
---|
474 | }
|
---|
475 | RTDbgModRelease(hCnt);
|
---|
476 | }
|
---|
477 | return rc;
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 |
|
---|
482 | /** Virtual function table for the DBGHELP debug info reader. */
|
---|
483 | DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDbgHelp =
|
---|
484 | {
|
---|
485 | /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
|
---|
486 | /*.fSupports = */ RT_DBGTYPE_CODEVIEW,
|
---|
487 | /*.pszName = */ "dbghelp",
|
---|
488 | /*.pfnTryOpen = */ rtDbgModDbgHelp_TryOpen,
|
---|
489 | /*.pfnClose = */ rtDbgModDbgHelp_Close,
|
---|
490 |
|
---|
491 | /*.pfnRvaToSegOff = */ rtDbgModDbgHelp_RvaToSegOff,
|
---|
492 | /*.pfnImageSize = */ rtDbgModDbgHelp_ImageSize,
|
---|
493 |
|
---|
494 | /*.pfnSegmentAdd = */ rtDbgModDbgHelp_SegmentAdd,
|
---|
495 | /*.pfnSegmentCount = */ rtDbgModDbgHelp_SegmentCount,
|
---|
496 | /*.pfnSegmentByIndex = */ rtDbgModDbgHelp_SegmentByIndex,
|
---|
497 |
|
---|
498 | /*.pfnSymbolAdd = */ rtDbgModDbgHelp_SymbolAdd,
|
---|
499 | /*.pfnSymbolCount = */ rtDbgModDbgHelp_SymbolCount,
|
---|
500 | /*.pfnSymbolByOrdinal = */ rtDbgModDbgHelp_SymbolByOrdinal,
|
---|
501 | /*.pfnSymbolByName = */ rtDbgModDbgHelp_SymbolByName,
|
---|
502 | /*.pfnSymbolByAddr = */ rtDbgModDbgHelp_SymbolByAddr,
|
---|
503 |
|
---|
504 | /*.pfnLineAdd = */ rtDbgModDbgHelp_LineAdd,
|
---|
505 | /*.pfnLineCount = */ rtDbgModDbgHelp_LineCount,
|
---|
506 | /*.pfnLineByOrdinal = */ rtDbgModDbgHelp_LineByOrdinal,
|
---|
507 | /*.pfnLineByAddr = */ rtDbgModDbgHelp_LineByAddr,
|
---|
508 |
|
---|
509 | /*.pfnUnwindFrame = */ rtDbgModDbgHelp_UnwindFrame,
|
---|
510 |
|
---|
511 | /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
|
---|
512 | };
|
---|
513 |
|
---|