VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmodldr.cpp@ 73455

Last change on this file since 73455 was 73412, checked in by vboxsync, 7 years ago

IPRT/dbgmoddeferred: Fixed debug close method using the wrong instance data pointer (img instead of dbg).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: dbgmodldr.cpp 73412 2018-07-31 16:50:04Z vboxsync $ */
2/** @file
3 * IPRT - Debug Module Image Interpretation by RTLdr.
4 */
5
6/*
7 * Copyright (C) 2011-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#include <iprt/dbg.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/err.h>
36#include <iprt/file.h>
37#include <iprt/ldr.h>
38#include <iprt/mem.h>
39#include <iprt/param.h>
40#include <iprt/path.h>
41#include <iprt/string.h>
42#include "internal/dbgmod.h"
43#include "internal/ldr.h"
44#include "internal/magics.h"
45
46
47/*********************************************************************************************************************************
48* Structures and Typedefs *
49*********************************************************************************************************************************/
50/**
51 * The instance data of the RTLdr based image reader.
52 */
53typedef struct RTDBGMODLDR
54{
55 /** Magic value (RTDBGMODLDR_MAGIC). */
56 uint32_t u32Magic;
57 /** The loader handle. */
58 RTLDRMOD hLdrMod;
59} RTDBGMODLDR;
60/** Pointer to instance data NM map reader. */
61typedef RTDBGMODLDR *PRTDBGMODLDR;
62
63
64
65/** @interface_method_impl{RTDBGMODVTIMG,pfnQueryProp} */
66static DECLCALLBACK(int) rtDbgModLdr_QueryProp(PRTDBGMODINT pMod, RTLDRPROP enmProp, void *pvBuf, size_t cbBuf, size_t *pcbRet)
67{
68 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
69 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
70 return RTLdrQueryPropEx(pThis->hLdrMod, enmProp, NULL /*pvBits*/, pvBuf, cbBuf, pcbRet);
71}
72
73
74/** @interface_method_impl{RTDBGMODVTIMG,pfnGetArch} */
75static DECLCALLBACK(RTLDRARCH) rtDbgModLdr_GetArch(PRTDBGMODINT pMod)
76{
77 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
78 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
79 return RTLdrGetArch(pThis->hLdrMod);
80}
81
82
83/** @interface_method_impl{RTDBGMODVTIMG,pfnGetFormat} */
84static DECLCALLBACK(RTLDRFMT) rtDbgModLdr_GetFormat(PRTDBGMODINT pMod)
85{
86 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
87 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
88 return RTLdrGetFormat(pThis->hLdrMod);
89}
90
91
92/** @interface_method_impl{RTDBGMODVTIMG,pfnReadAt} */
93static DECLCALLBACK(int) rtDbgModLdr_ReadAt(PRTDBGMODINT pMod, uint32_t iDbgInfoHint, RTFOFF off, void *pvBuf, size_t cb)
94{
95 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
96 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
97 RT_NOREF_PV(iDbgInfoHint);
98 return rtLdrReadAt(pThis->hLdrMod, pvBuf, UINT32_MAX /** @todo iDbgInfo*/, off, cb);
99}
100
101
102/** @interface_method_impl{RTDBGMODVTIMG,pfnUnmapPart} */
103static DECLCALLBACK(int) rtDbgModLdr_UnmapPart(PRTDBGMODINT pMod, size_t cb, void const **ppvMap)
104{
105 Assert(((PRTDBGMODLDR)pMod->pvImgPriv)->u32Magic == RTDBGMODLDR_MAGIC);
106 NOREF(pMod); NOREF(cb);
107 RTMemFree((void *)*ppvMap);
108 *ppvMap = NULL;
109 return VINF_SUCCESS;
110}
111
112
113/** @interface_method_impl{RTDBGMODVTIMG,pfnMapPart} */
114static DECLCALLBACK(int) rtDbgModLdr_MapPart(PRTDBGMODINT pMod, uint32_t iDbgInfo, RTFOFF off, size_t cb, void const **ppvMap)
115{
116 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
117 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
118
119 void *pvMap = RTMemAlloc(cb);
120 if (!pvMap)
121 return VERR_NO_MEMORY;
122
123 int rc = rtLdrReadAt(pThis->hLdrMod, pvMap, iDbgInfo, off, cb);
124 if (RT_SUCCESS(rc))
125 *ppvMap = pvMap;
126 else
127 {
128 RTMemFree(pvMap);
129 *ppvMap = NULL;
130 }
131 return rc;
132}
133
134
135/** @interface_method_impl{RTDBGMODVTIMG,pfnImageSize} */
136static DECLCALLBACK(RTUINTPTR) rtDbgModLdr_ImageSize(PRTDBGMODINT pMod)
137{
138 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
139 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
140 return RTLdrSize(pThis->hLdrMod);
141}
142
143
144/** @interface_method_impl{RTDBGMODVTIMG,pfnRvaToSegOffset} */
145static DECLCALLBACK(int) rtDbgModLdr_RvaToSegOffset(PRTDBGMODINT pMod, RTLDRADDR Rva, PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
146{
147 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
148 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
149 return RTLdrRvaToSegOffset(pThis->hLdrMod, Rva, piSeg, poffSeg);
150}
151
152
153/** @interface_method_impl{RTDBGMODVTIMG,pfnLinkAddressToSegOffset} */
154static DECLCALLBACK(int) rtDbgModLdr_LinkAddressToSegOffset(PRTDBGMODINT pMod, RTLDRADDR LinkAddress,
155 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
156{
157 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
158 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
159 return RTLdrLinkAddressToSegOffset(pThis->hLdrMod, LinkAddress, piSeg, poffSeg);
160}
161
162
163/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSymbols} */
164static DECLCALLBACK(int) rtDbgModLdr_EnumSymbols(PRTDBGMODINT pMod, uint32_t fFlags, RTLDRADDR BaseAddress,
165 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
166{
167 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
168 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
169 return RTLdrEnumSymbols(pThis->hLdrMod, fFlags, NULL /*pvBits*/, BaseAddress, pfnCallback, pvUser);
170}
171
172
173/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumSegments} */
174static DECLCALLBACK(int) rtDbgModLdr_EnumSegments(PRTDBGMODINT pMod, PFNRTLDRENUMSEGS pfnCallback, void *pvUser)
175{
176 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
177 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
178 return RTLdrEnumSegments(pThis->hLdrMod, pfnCallback, pvUser);
179}
180
181
182/** @interface_method_impl{RTDBGMODVTIMG,pfnEnumDbgInfo} */
183static DECLCALLBACK(int) rtDbgModLdr_EnumDbgInfo(PRTDBGMODINT pMod, PFNRTLDRENUMDBG pfnCallback, void *pvUser)
184{
185 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
186 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
187 return RTLdrEnumDbgInfo(pThis->hLdrMod, NULL, pfnCallback, pvUser);
188}
189
190
191/** @interface_method_impl{RTDBGMODVTIMG,pfnClose} */
192static DECLCALLBACK(int) rtDbgModLdr_Close(PRTDBGMODINT pMod)
193{
194 PRTDBGMODLDR pThis = (PRTDBGMODLDR)pMod->pvImgPriv;
195 AssertPtr(pThis);
196 Assert(pThis->u32Magic == RTDBGMODLDR_MAGIC);
197
198 int rc = RTLdrClose(pThis->hLdrMod); AssertRC(rc);
199 pThis->hLdrMod = NIL_RTLDRMOD;
200 pThis->u32Magic = RTDBGMODLDR_MAGIC_DEAD;
201
202 RTMemFree(pThis);
203
204 return VINF_SUCCESS;
205}
206
207
208/** @interface_method_impl{RTDBGMODVTIMG,pfnTryOpen} */
209static DECLCALLBACK(int) rtDbgModLdr_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
210{
211 RTLDRMOD hLdrMod;
212 int rc = RTLdrOpen(pMod->pszImgFile, RTLDR_O_FOR_DEBUG, enmArch, &hLdrMod);
213 if (RT_SUCCESS(rc))
214 {
215 rc = rtDbgModLdrOpenFromHandle(pMod, hLdrMod);
216 if (RT_FAILURE(rc))
217 RTLdrClose(hLdrMod);
218 }
219 return rc;
220}
221
222
223/** Virtual function table for the RTLdr based image reader. */
224DECL_HIDDEN_CONST(RTDBGMODVTIMG) const g_rtDbgModVtImgLdr =
225{
226 /*.u32Magic = */ RTDBGMODVTIMG_MAGIC,
227 /*.fReserved = */ 0,
228 /*.pszName = */ "RTLdr",
229 /*.pfnTryOpen = */ rtDbgModLdr_TryOpen,
230 /*.pfnClose = */ rtDbgModLdr_Close,
231 /*.pfnEnumDbgInfo = */ rtDbgModLdr_EnumDbgInfo,
232 /*.pfnEnumSegments = */ rtDbgModLdr_EnumSegments,
233 /*.pfnEnumSymbols = */ rtDbgModLdr_EnumSymbols,
234 /*.pfnImageSize = */ rtDbgModLdr_ImageSize,
235 /*.pfnLinkAddressToSegOffset = */ rtDbgModLdr_LinkAddressToSegOffset,
236 /*.pfnRvaToSegOffset= */ rtDbgModLdr_RvaToSegOffset,
237 /*.pfnMapPart = */ rtDbgModLdr_MapPart,
238 /*.pfnUnmapPart = */ rtDbgModLdr_UnmapPart,
239 /*.pfnReadAt = */ rtDbgModLdr_ReadAt,
240 /*.pfnGetFormat = */ rtDbgModLdr_GetFormat,
241 /*.pfnGetArch = */ rtDbgModLdr_GetArch,
242 /*.pfnQueryProp = */ rtDbgModLdr_QueryProp,
243
244 /*.u32EndMagic = */ RTDBGMODVTIMG_MAGIC
245};
246
247
248/**
249 * Open PE-image trick.
250 *
251 * @returns IPRT status code
252 * @param pDbgMod The debug module instance.
253 * @param hLdrMod The module to open a image debug backend for.
254 */
255DECLHIDDEN(int) rtDbgModLdrOpenFromHandle(PRTDBGMODINT pDbgMod, RTLDRMOD hLdrMod)
256{
257 PRTDBGMODLDR pThis = (PRTDBGMODLDR)RTMemAllocZ(sizeof(RTDBGMODLDR));
258 if (!pThis)
259 return VERR_NO_MEMORY;
260
261 pThis->u32Magic = RTDBGMODLDR_MAGIC;
262 pThis->hLdrMod = hLdrMod;
263 pDbgMod->pvImgPriv = pThis;
264 return VINF_SUCCESS;
265}
266
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