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