VirtualBox

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

Last change on this file since 57974 was 57974, checked in by vboxsync, 9 years ago

IPRT: Some more doxygen fixes.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette